`
lz726
  • 浏览: 328758 次
  • 性别: Icon_minigender_2
  • 来自: 福建,福州
社区版块
存档分类
最新评论

文件(或者说附件)上传下载(单个)

阅读更多
单个上传,普通的form,普通的file:下面的是用来实现多个的,但是为了实现单个就只循环一次,不知道还有没别的办法~
  1.     String uploadPath = "c:/";   
  2.     if(ServletFileUpload.isMultipartContent(request))   
  3.     {   
  4.         System.out.println("编码方式为:"+request.getCharacterEncoding());   
  5.         DiskFileItemFactory factory = new DiskFileItemFactory();   
  6.         factory.setRepository(new File(uploadPath));   
  7.         ServletFileUpload sfu = new ServletFileUpload(factory);   
  8.         sfu.setSizeMax(ConfigBean.getIntValue("permitFileSize"));   
  9.         List list=null;   
  10.         try {   
  11.             list = sfu.parseRequest(request);   
  12.         } catch (FileUploadException e1) {   
  13.                
  14.             e1.printStackTrace();   
  15.         }   
  16.         Iterator it = list.iterator();   
  17.         int num = 0;   
  18.         while(it.hasNext())   
  19.         {   
  20.             FileItem fi = (FileItem) it.next();   
  21.                
  22.             if(fi.isFormField())   
  23.             {   
  24.                 System.out.println(fi.getFieldName());    
  25.             }   
  26.             else  
  27.             {   
  28.                 String fileName = fi.getName();   
  29.                 fileName = fileName.substring(fileName.lastIndexOf("\\")+1);  
  30.                 byte[] b = fi.get();  
  31.                 String webPath  =uploadPath+"\\"+fileName;  
  32.                 request.setAttribute("webPath",webPath);   
  33.                 BufferedOutputStream bos;   
  34.                 try {   
  35.                     bos = new BufferedOutputStream(new FileOutputStream(webPath));   
  36.                     bos.write(b);   
  37.                     bos.close();   
  38.                 } catch (FileNotFoundException e) {   
  39.                        
  40.                     e.printStackTrace();   
  41.                 } catch (IOException e) {   
  42.                        
  43.                     e.printStackTrace();   
  44.                 }   
  45.                 break;   
  46.             }   
  47.                
  48.                
  49.         }   
  50.            
  51.            
  52.     }  

 

下载的:

代码是在这里找到的.呵呵~
  1. OutputStream out;   
  2.         try {   
  3.             out = response.getOutputStream();   
  4.             String filename = request.getParameter("filename");       
  5.             String filepath = "c:/"+filename;    
  6.             response.setContentType("application/octet-stream");     
  7.             response.setHeader("Content-Disposition","attachment;filename = "+filename);       
  8.             java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath);      
  9.             int c;   
  10.             byte[] buff = new byte[1000];   
  11.             while ((c = fileInputStream.read(buff, 01000)) > 0) {   
  12.                 out.write(buff, 0, c);   
  13.                 out.flush();   
  14.             }   
  15.             out.flush();   
  16.             out.close();   
  17.             fileInputStream.close();    
  18.                
  19.         } catch (IOException e) {   
  20.                
  21.                
  22.             e.printStackTrace();   
  23.         }   
  24.           

 

界面上用一个连接:

xml 代码
  1. <a  href='<%=webPath%>/chat/file.shtml?method=downLoad&filename='<!---->  title='点击接收' >文件名a>  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics