plupload上传插件在SpringMVC中的整合
插件地址:http://www.plupload.com/
多附件上传控件plupload的使用心得http://panyongzheng.iteye.com/blog/1943644
引入:
<link rel="stylesheet" href="<c:url value="/scripts/lib/plupload/jquery.ui.plupload/css/jquery.ui.plupload.css"/>"/> <link rel="stylesheet" href="<c:url value="/scripts/lib/plupload/jquery.plupload.queue/css/jquery.plupload.queue.css"/>"/> <script type="text/javascript" src="<c:url value="/scripts/lib/plupload/plupload.full.min.js"/>"></script> <script type="text/javascript" src="<c:url value="/scripts/lib/plupload/moxie.js"/>"></script> <script type="text/javascript" src="<c:url value="/scripts/lib/plupload/jquery.ui.plupload/jquery.ui.plupload.js"/>"></script> <script type="text/javascript" src="<c:url value="/scripts/lib/plupload/jquery.plupload.queue/jquery.plupload.queue.js"/>"></script> <script type="text/javascript" src="<c:url value="/scripts/lib/plupload/i18n/zh_CN.js"/>"></script>
前台创建:
methods.uploader = $("#divRhCpmUploadDialog_uploader").pluploadQueue({ // General settings runtimes : 'html5,flash,silverlight,html4', url : globalContentPath+"/updown/u", chunk_size : '1mb', rename : true, dragdrop: true, unique_names:true, filters : { // Maximum file size max_file_size : '10mb', // Specify what files to browse for mime_types: [ {title : "Image files", extensions : "jpg,gif,png"}, {title : "Zip files", extensions : "zip"} ] }, // Resize images on clientside if we can resize: { width : 200, height : 200, quality : 90, crop: true // crop to exact dimensions }, // Flash settings flash_swf_url : '/plupload/js/Moxie.swf', // Silverlight settings silverlight_xap_url : '/plupload/js/Moxie.xap', init:{//实现方法 FilesAdded:function(up, files) { debugger; for (var i in files) { up.getFile(files[i].id); console.log("AAAAAAAAA"); } }, UploadComplete: function(up, files) { methods.files=files; } } });
http://blog.csdn.net/t894690230/article/details/37956277
上传对象
package cn.com.voge.system.domain; /** * 项目名称: wp_idea_linux * 功能说明: * 创建者: Pandy, * 邮箱: [email protected], [email protected] * 版权: * 官网: * 创建日期: 15-6-2. * 创建时间: 下午1:31. * 修改历史: * ----------------------------------------------- */ import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; /** * Plupload是一个上传插件。 * 这是一个bean类,主要存储Plupload插件上传时需要的参数。 * 属性名不可随意改动. * 这里主要使用MultipartFile文件上传方法 */ public class Plupload { /**文件临时名(打文件被分解时)或原名*/ private String name; /**总的块数*/ private int chunks = -1; /**当前块数(从0开始计数)*/ private int chunk = -1; /**HttpServletRequest对象,不能直接传入进来,需要手动传入*/ private HttpServletRequest request; /**保存文件上传信息,不能直接传入进来,需要手动传入*/ private MultipartFile multipartFile; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getChunks() { return chunks; } public void setChunks(int chunks) { this.chunks = chunks; } public int getChunk() { return chunk; } public void setChunk(int chunk) { this.chunk = chunk; } public HttpServletRequest getRequest() { return request; } public void setRequest(HttpServletRequest request) { this.request = request; } public MultipartFile getMultipartFile() { return multipartFile; } public void setMultipartFile(MultipartFile multipartFile) { this.multipartFile = multipartFile; } }
上传工具类
package cn.com.voge.system.domain; /** * 项目名称: wp_idea_linux * 功能说明: * 创建者: Pandy, * 邮箱: [email protected], [email protected] * 版权: * 官网: * 创建日期: 15-6-2. * 创建时间: 下午1:30. * 修改历史: * ----------------------------------------------- */ import org.springframework.util.MultiValueMap; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import java.io.*; import java.util.Iterator; import java.util.List; /** * Plupload是一个上传插件。 * 上传原理为单个文件依次发送至服务器. * 上传打文件时可以将其碎片化上传。但是一般情况下,不会这样做, * 所以这里更多的是处理普通文件的批量上传。 * 这里主要处理文件上传 */ public class PluploadUtil { private static final int BUF_SIZE = 2 * 1024; /**上传失败响应的成功状态码*/ public static final String RESP_SUCCESS = "{\"jsonrpc\" : \"2.0\", \"result\" : \"success\", \"id\" : \"id\"}"; /**上传失败响应的失败状态码*/ public static final String RESP_ERROR = "{\"jsonrpc\" : \"2.0\", \"error\" : {\"code\": 101, \"message\": \"Failed to open input stream.\"}, \"id\" : \"id\"}"; /** * 用于Plupload插件的文件上传,自动生成唯一的文件保存名 * @param plupload - 存放上传所需参数的bean * @param dir - 保存目标文件目录 * @throws IllegalStateException * @throws IOException */ public static void upload(Plupload plupload, File dir) throws IllegalStateException, IOException { //生成唯一的文件名 String filename = "" + System.currentTimeMillis() + plupload.getName(); upload(plupload, dir, filename); } /** * 用于Plupload插件的文件上传 * @param plupload - 存放上传所需参数的bean * @param dir - 保存目标文件目录 * @param filename - 保存的文件名 * @throws IllegalStateException * @throws IOException */ public static void upload(Plupload plupload, File dir, String filename) throws IllegalStateException, IOException { int chunks = plupload.getChunks(); //获取总的碎片数 int chunk = plupload.getChunk(); //获取当前碎片(从0开始计数) System.out.println(plupload.getMultipartFile() + "----------"); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) plupload.getRequest(); MultiValueMap<String, MultipartFile> map = multipartRequest.getMultiFileMap(); if(map != null) { if (!dir.exists()) dir.mkdirs(); //如果目标文件夹不存在则创建新的文件夹 //事实上迭代器中只存在一个值,所以只需要返回一个值即可 Iterator<String> iter = map.keySet().iterator(); while(iter.hasNext()) { String str = (String) iter.next(); List<MultipartFile> fileList = map.get(str); for(MultipartFile multipartFile : fileList) { //因为只存在一个值,所以最后返回的既是第一个也是最后一个值 plupload.setMultipartFile(multipartFile); //创建新目标文件 File targetFile = new File(dir.getPath()+ "/" + filename); //当chunks>1则说明当前传的文件为一块碎片,需要合并 if (chunks > 1) { //需要创建临时文件名,最后再更改名称 File tempFile = new File(dir.getPath()+ "/" + multipartFile.getName()); //如果chunk==0,则代表第一块碎片,不需要合并 saveUploadFile(multipartFile.getInputStream(), tempFile, chunk == 0 ? false : true); //上传并合并完成,则将临时名称更改为指定名称 if (chunks - chunk == 1) { tempFile.renameTo(targetFile); } } else { //否则直接将文件内容拷贝至新文件 multipartFile.transferTo(targetFile); } } } } } /** * 保存上传文件,兼合并功能 */ private static void saveUploadFile(InputStream input, File targetFile, boolean append) throws IOException { OutputStream out = null; try { if (targetFile.exists() && append) { out = new BufferedOutputStream(new FileOutputStream(targetFile, true), BUF_SIZE); } else { out = new BufferedOutputStream(new FileOutputStream(targetFile), BUF_SIZE); } byte[] buffer = new byte[BUF_SIZE]; int len = 0; //写入文件 while ((len = input.read(buffer)) > 0) { out.write(buffer, 0, len); } } catch (IOException e) { throw e; } finally { //关闭输入输出流 if (null != input) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != out) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 判断是否全部上传完成 * 碎片需合并后才返回真 */ public static boolean isUploadFinish(Plupload plupload) { return (plupload.getChunks() - plupload.getChunk() == 1); } }
controller
@ResponseBody @RequestMapping(value = "/u", method = RequestMethod.POST) public String upload(Plupload plupload,HttpServletRequest request, HttpServletResponse response) { String FileDir = "uploadfile/"; plupload.setRequest(request); //文件存储路径 File dir = new File(plupload.getRequest().getSession().getServletContext().getRealPath("/") + FileDir); System.out.println(dir.getPath()); try { //上传文件 PluploadUtil.upload(plupload, dir); //判断文件是否上传成功(被分成块的文件是否全部上传完成) if (PluploadUtil.isUploadFinish(plupload)) { System.out.println(plupload.getName() + "----"); } } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "login.upload"; }
相关推荐
Tjhfsghbjknjdy 2018-08-20
zjnig的信息仓库 2018-08-20
学留痕 2015-05-21
wolaoreme 2013-07-19
zyb00yaonuli 2011-08-04
qixiang0 2017-07-03
zangshuge 2016-07-19
88284453 2017-04-19
快意江湖 2019-04-25
ThinkPHP 2017-11-03
phpyounger 2019-04-15
nnj 2011-09-04