ssh头像上传
1. SSH 图片上传问题 java.lang.NullPointerException myFileName为空,那个图片的名字获取不过来
private File myFile;
private String myFileName; ->应该为 private String myFileFileName;
上传时文件名应该是 文件+“FileName” 在这里就是 myFile+“FileName”
2. ssh 上传图片 只保存图片名称在数据库怎么做
这很简单啊,一般情况下上传文件都是提供一个文件选择框,也就是<input type="file" />给用户,用户选择文件后,浏览器会自动将文件名显示在文本框中,你只要将这个文件名取出来然后保存到数据库中就可以了
3. ssh上传图片的问题
存链接,图片存硬盘里的
4. 在SSH框架中建表照片用什么类型,就想写个学生信息系统,然后上传学生头像
用varchar存地址不就行了!
5. ssh框架上传图片与显示
页面的form
<formaction="indexDetail.action"method="post"enctype="multipart/form-data">
图片:<inputtype="file"name="file"/><br/>
<inputtype="submit"name="imageField"class="btn_imgimg_submit"value="提交"/>
</form>
后台代码
//上传文件
privateFilefile;
privateStringfileFileName;
Stringrealpath=ServletActionContext.getServletContext().getRealPath("/images/ask/");
if(!newFile(realpath).exists()){
newFile(realpath).mkdir();
}
String[]typechoose=fileFileName.split("\.");
intichoose=typechoose.length;
Stringtype=ichoose>1?typechoose[ichoose-1]:"";
SimpleDateFormatsmat=newSimpleDateFormat("yyyyMMddHHmmss");
Stringnewfilname=smat.format(newDate())+"."+type;
Stringpath=realpath+"/"+newfilname;
FileUtil.saveFile(path,file);
核心代码都在里面
6. SSH2框架图片上传到数据库并显示在JSP页面
//传统的struts2上传是很简单的。
//页面form提交到action:
//这里使用集合,页面提交过来的n个inputname=“file”的文件将会被装进去,如果只上传一
//个文件可以直接声明成:privateFilefile、StringfileFileName、StringfileContentType
privateList<File>file;
privateList<String>fileFileName;
privateList<String>fileContentType;
publicList<File>getFile(){
returnfile;
}
publicvoidsetFile(List<File>file){
this.file=file;
}
publicList<String>getFileFileName(){
returnfileFileName;
}
publicvoidsetFileFileName(List<String>fileFileName){
this.fileFileName=fileFileName;
}
publicList<String>getFileContentType(){
returnfileContentType;
}
publicvoidsetFileContentType(List<String>fileContentType){
this.fileContentType=fileContentType;
}
publicStringexecute()throwsException{
List<String>s=newArrayList<String>();
for(inti=0;i<file.size();i++){
InputStreamis=newFileInputStream(file.get(i));
//在webroot先建立个upload文件夹,也可以用代码创建,这里为了简便,就直接使用了
Stringroot=ServletActionContext.getRequest().getRealPath("/upload");
FiledistFile=newFile(root,this.getFileFileName().get(i));
OutputStreamos=newFileOutputStream(distFile);
byte[]buffer=newbyte[400];
intlength=0;
while((length=is.read(buffer))>0){
os.write(buffer,0,length);
}
is.close();
os.close();
//数据库存放以下路径,当需要在页面显示,直接提取出来用IMG标签装载即可
StringnewFilePath=root+"/"+distFile.getFileName();
}
returnSUCCESS;
}
//的代码不需要很复杂,简单的执行数据库插入就好。
7. ssh框架中图片无法上传了,怎么解决
也许地址有问题。你自己看看地址。还有就是在配置文件中没设置
8. 能不能个我一段 SSH 上传图片的代码!只将图片的路径保存到数据库中!
给你一段!希望能帮到你。
/**
* @see跳转方法
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IOException
*/
public ActionForward toadd(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws IOException
{
HoteForm hoteForm = (HoteForm) form;
saveToken(request);//生成一个TOKEN
return mapping.findForward("add");
}
/**
* @see执行添加方法
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IOException
*/
public ActionForward doadd(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws IOException
{
HoteForm hoteForm = (HoteForm) form;
if(this.isTokenValid(request,true))
{
//上传文件
String dir = this.getServlet().getServletContext().getRealPath("/upLoadFiles/hoteimg");
if(form instanceof HoteForm)
{
FormFile file = hoteForm.getTheFile();
//定义图片格式
List<String> list = new ArrayList();
list.add("jpg");
list.add("jpeg");
list.add("gif");
list.add("bmp");
if(!dir.endsWith("/"))
{
dir = dir.concat("/");
if(file != null)
{
String fileName = file.getFileName();//获取文件名
int fsize = file.getFileSize();//获取文件大小
String ext = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());// 获取文件类型,即扩展名
fileName = getdateRenameFileName()+"."+ext;//文件重命名
ext = ext.toLowerCase();//转换为小写
//判断是否是要求的上传的类型图片
if(!list.contains(ext))
{
response.getWriter().print("<script>alert('不支持该文件!');location.href='hote.do?operate=toadd'</script>");
return null;
}
if(fsize > 1024*1024)
{
response.getWriter().print("<script>alert('文件过大!');location.href='hote.do?operate=toadd'</script>");
return null;
}
try {
InputStream stream = file.getInputStream();
OutputStream bos = new FileOutputStream(dir+"/"+fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while((bytesRead = stream.read(buffer,0,8192)) != -1)
{
bos.write(buffer,0,bytesRead);
}
bos.close();
} catch (Exception e) {
// TODO: handle exception
}
file.destroy();
hoteForm.getHote().setImage(fileName);
}
}
}
// 获取系统当前时间
java.util.Date date = Calendar.getInstance().getTime();
// 时间格式化 年-月-日
String time = new SimpleDateFormat("yyyy-MM-dd").format(date);
hoteForm.getHote().setTime(time);
Users user = (Users)request.getSession().getAttribute("user");
hoteForm.getHote().setUsers(user);
if(hoteForm.getHote() != null)
{
if(this.hoteBiz.add(hoteForm.getHote()))
{
response.getWriter().print("<script> alert('添加成功!');location.href='hote.do?operate=getHoteList'</script>");
}
}
response.getWriter().print("<script> alert('添加失败!');javascript:history.go(-1);</script>");
}else
{
response.getWriter().print("<script> alert('不能重复提交数据!');javascript:history.go(-1);</script>");
}
return null;
}
/**
* @see图片重命名
* @return
*/
public String getdateRenameFileName() {
java.text.SimpleDateFormat formatter_f = new java.text.SimpleDateFormat(
"yyyyMMddHHmmss");
java.util.Date currentTime_f = new java.util.Date(); // 得到当前系统时间
String new_date_f = formatter_f.format(currentTime_f); // 将日期时间格式化
return new_date_f;
}
9. 求一段 SSH上传图片的代码,只需要把路径存到数据库中即可
UploadActionForm uaf = (UploadActionForm)form; ///UploadActionForm 是封装了formbean的java类
System.out.println("title:"+uaf.getTitle());
FormFile myFile = uaf.getMyfile();
if(null != myFile) {
System.out.println("fileName:"+myFile.getFileName());
FileOutputStream fos = new FileOutputStream ("d:\\+你要上传到服务器的路径);
fos.write(myFile.getFileData());
fos.flush();
fos.close();
}
10. ssh2使用fileuoload上传图片提示问题!上传成功了但不知怎么提示上传图片过大!
要限制大小,在配置中配置就可以。。。。
上传到哪,由自己决定,因为,上传后得到文件和输入流了