当前位置: 代码迷 >> Eclipse >> jsp怎样把图片上传到文件夹而不存到数据库解决方案
  详细解决方案

jsp怎样把图片上传到文件夹而不存到数据库解决方案

热度:80   发布时间:2016-04-23 13:30:56.0
jsp怎样把图片上传到文件夹而不存到数据库
jsp怎样把图片上传到文件夹而不存到数据库,当然数据库里面存的是相对路径 我用ssh框架写的 谁有这方面的小例子 请给我一份,谢谢了

------解决方案--------------------
UpForm upForm = (UpForm) form;
try {
InputStream in = ff.getInputStream();
String dir = this.getServlet().getServletContext().getRealPath("/WEB-INF/ss/s");
File f = new File(dir);//+"\\"+ff.getFileName());
if(!f.exists()){
f.mkdirs();
}
f = new File(dir,ff.getFileName());
FileOutputStream fos = new FileOutputStream(f);
byte [] buf = new byte[1024];
int i = 0;
while((i=in.read(buf)) != -1){
fos.write(buf, 0, i);
}
in.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
response.sendRedirect("a.jsp");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

------解决方案--------------------
Java code
struts1 多文件上传.JSP页面就可以<input type=file name=xx/>多个,不过,上传的图片如何显示呢?恐怕不能显示在文字中间了.这个不好控制.可以使用在线编辑器上传,先用一个IFRAME异步上传图片,然后得到相对路径.把路径放到文章里.就可以了EmailForm ef=(EmailForm)form;         Hashtable uploadFiles=ef.getMultipartRequestHandler().getFileElements();         //已经上传到服务器的磁盘文件列表,将文件写到磁盘         List<File> diskFileList=new ArrayList<File>();         String path=this.servlet.getServletContext().getRealPath("/");         String uploadFilePath=path+"/UploadFile/";         Calendar cl=Calendar.getInstance();         String fileDir=cl.get(Calendar.YEAR)+"/"+(cl.get(Calendar.MONTH)+1)+"/"+cl.get(Calendar.DAY_OF_MONTH)+"/";         if(uploadFiles.size()>0){            Enumeration fk=uploadFiles.keys();            while(fk.hasMoreElements()){               String fname=(String)fk.nextElement();               FormFile ff=(FormFile)uploadFiles.get(fname);               int size=ff.getFileSize();               if(size>0){ //不加判断,如果上传文件选择框里没有选择文件会出错.                  if(size>20*1024*1024){                     ActionMessages errors=new ActionMessages();                     errors.add("error.file.filetolarge",new ActionMessage("error.file.filetolarge"));                     saveErrors(request,errors);                     return mapping.findForward("error");                  }else{//下面这个updown是上传文件的.在下面.                     diskFileList.add(officeImpl.updown(ff,uploadFilePath+fileDir));                  }               }            }         }public File updown(FormFile pic,String ph) throws IOException{      String fname=pic.getFileName();      File picStoreDir=new File(ph);      picStoreDir.mkdirs(); //建立文件夹      fname=System.currentTimeMillis()+fname.substring(fname.indexOf("."));      String path=ph+fname;      File f=null;      InputStream stream=null;      OutputStream fos=null;      try{         //FileInputStream in=new FileInputStream(pic.getPath());         stream=pic.getInputStream(); // 把文件读入         fos=new BufferedOutputStream(new FileOutputStream(path)); // 建立上传文件输出流         int sRead=0;         byte[] buffer=new byte[8192];         while((sRead=stream.read(buffer,0,8192))!=-1){            fos.write(buffer,0,sRead); // 将文件写入服务器         }         f=new File(path);      }catch(Exception e){         System.err.print(e);      }finally{         stream.close();         fos.close();      }      return f;   }
  相关解决方案