`
312350968
  • 浏览: 209274 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

plupload上传

 
阅读更多

来源:http://asyty.iteye.com/blog/1230119/

 

最近需要用到jquery文件上传插件,发现plupload这东西挺好的,奈何后台代码是php,tomcat得配置php才能跑起来,于是稍微研究了下,改成了java代码

 

plupload的特色是

1、可以配置chunk,将一个大文件分成许多小文件上传,后台通过php合并成大文件,这里通过相应的java代码

2、实际上传的文件名是经过生成唯一的uuid,通过参数name传递到后台

3、上传文件的过程是先上传临时命名为uuid的临时文件,上传成功后会自动生成几个input标签,对应上传之后的临时文件的文件名,之后通过另一个action调用uploadFinish对临时文件进行重命名 操作或者其他操作

 

 

 

这个java代码是基于 Struts2的,不是servlet,反正都是类似的 在这基础上也容易改

 

 

Java代码  收藏代码
  1. public class UploadAction extends ActionSupport {  
  2.   
  3.   
  4.        private static final int BUFFER_SIZE = 2 * 1024;  
  5.   
  6.   
  7.     private File upload;  
  8.         private String name;  //plupload上传文件的临时文件名 uuid.文件后缀  
  9.     private String uploadFileName;  
  10.     private String uploadContentType;  
  11.     private int chunk;  
  12.     private int chunks;  
  13.   
  14. // 。。。一堆getter setter自己生成  
  15.   
  16.   
  17.   
  18.         private void copy(File src, File dst) {  
  19.         InputStream in = null;  
  20.         OutputStream out = null;  
  21.         try {  
  22.             if (dst.exists()) {  
  23.                 out = new BufferedOutputStream(new FileOutputStream(dst, true),  
  24.                         BUFFER_SIZE);  //plupload 配置了chunk的时候新上传的文件appand到文件末尾  
  25.             } else {  
  26.                 out = new BufferedOutputStream(new FileOutputStream(dst),  
  27.                         BUFFER_SIZE);  
  28.             }  
  29.             in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);  
  30.   
  31.             byte[] buffer = new byte[BUFFER_SIZE];  
  32.             int len = 0;  
  33.             while ((len = in.read(buffer)) > 0) {  
  34.                 out.write(buffer, 0, len);  
  35.             }  
  36.         } catch (Exception e) {  
  37.             e.printStackTrace();  
  38.         } finally {  
  39.             if (null != in) {  
  40.                 try {  
  41.                     in.close();  
  42.                 } catch (IOException e) {  
  43.                     e.printStackTrace();  
  44.                 }  
  45.             }  
  46.             if (null != out) {  
  47.                 try {  
  48.                     out.close();  
  49.                 } catch (IOException e) {  
  50.                     e.printStackTrace();  
  51.                 }  
  52.             }  
  53.         }  
  54.     }  
  55.   
  56.   
  57.   
  58.         public String upload() throws Exception {  
  59.   
  60.         String dstPath = ServletActionContext.getServletContext().getRealPath("\\tmp")  
  61.                 + "\\" + this.getName();  // 保存目录可以自己配置 或者定义变量自行配置  
  62.         File dstFile = new File(dstPath);  
  63.   
  64.         // 文件已存在删除旧文件(上传了同名的文件)  
  65.         if (chunk == 0 && dstFile.exists()) {  
  66.             dstFile.delete();  
  67.             dstFile = new File(dstPath);  
  68.         }  
  69.   
  70.         copy(this.upload, dstFile);  
  71.   
  72.                //System.out.println(uploadFileName + " " + uploadContentType + " "  
  73.                 + chunk + " " + chunks);  
  74.         if (chunk == chunks - 1) {  
  75.             // 一个完整的文件上传完成  
  76.         }  
  77.   
  78.         return SUCCESS;  
  79.     }  
  80.   
  81.     public String uploadFinish() {  
  82.         String dstPath = ServletActionContext.getServletContext().getRealPath("\\tmp");  
  83.   
  84.         HttpServletRequest request = ServletActionContext.getRequest();  
  85.           
  86.         int count = Integer.parseInt(request.getParameter("uploader_count"));  
  87.         for (int i = 0; i < count; i++) {  
  88.             uploadFileName = request.getParameter("uploader_" + i + "_name");  
  89.             name = request.getParameter("uploader_" + i + "_tmpname");  
  90.             System.out.println(uploadFileName + " " + name);  
  91.             try {  
  92.   
  93. //对已经上传成功的临时文件进行操作        
  94.                         } catch(Exception e) {  
  95.                   
  96.             }  
  97.         }  
  98.         return SUCCESS;  
  99.     }  
  100.   
  101. }  

补充:

从项目里抽取出了文件上传的代码,单独一个eclipse上可以跑的例子,上传上来了(注意build path里的jre路径)

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics