1.前端

(1)依赖文件:

    <link type="text/css" rel="stylesheet" href="~/Content/plupload_2_1_2/jquery-ui.min.css" media="screen" /><link type="text/css" rel="stylesheet" href="~/Content/plupload_2_1_2/jquery.ui.plupload/css/jquery.ui.plupload.css" media="screen" /><script type="text/javascript" src="~/Content/plupload_2_1_2/jquery.js" charset="UTF-8"></script><script type="text/javascript" src="~/Content/plupload_2_1_2/jquery-ui.min.js" charset="UTF-8"></script><script type="text/javascript" src="~/Content/plupload_2_1_2/plupload.full.min.js" charset="UTF-8"></script><script type="text/javascript" src="~/Content/plupload_2_1_2/jquery.ui.plupload/jquery.ui.plupload.min.js" ></script><script type="text/javascript" src="~/Content/plupload_2_1_2/zh_CN.js" charset="UTF-8"></script>

(2)HTML

    <form id="uploader"><input type="text" name="filename" value="" /></form>

(3)js

   <script type="text/javascript">//文件名不能相同
        $(function () {$("#uploader").plupload({// General settings
                runtimes: 'gears,flash,silverlight,browserplus,html5',url: '/upload/plupload',max_file_size: '6079mb',max_file_count: 20,chunk_size: '2048kb',unique_names: false,init: {// Resize images on clientside if we can//resize: { width: 320, height: 240, quality: 90 },
                    rename: false, // 是否重命名文件
                    sortable: true, // Sort files
                    dragdrop: true, //启用文件到小部件能够拖放(操作)(目前唯一HTML5支持)
                    filters: [{ title: "Image files", extensions: "jpg,gif,png,txt,sql" },{ title: "music", extensions: "avi,mp4,mp3,exe,zip" }],views: {list: true,thumbs: true, // Show thumbs
                        active: 'thumbs'},// Flash settings
                    flash_xap_url: '~/Content/upload/js/Moxie.xap',// Silverlight settings
                    silverlight_xap_url: '~/Content/upload/js/Moxie.xap',BeforeUpload: function (uploader, file) {//alert(123)//var $li = $('#uploader_filelist li').hasClass('plupload_file')[0]//var value = $($li).find('div.plupload_file_name').attr('title')//alert($li)
                    },QueueChanged: function () {var $li = $('#uploader_filelist li')console.log($li)},FileUploaded: function (uploader, file, responseObject) {//console.log(responseObject)//alert(curDirID)//initData(responseObject.response.curDirId)
                        $.ajax({url: '/Cloudpan/GetCurdir',type: 'post',success: function (id) {initData(id)}})}}});});</script>

View Code

2.后台

  后台可以使用FileInputStream的构造方法追加文件内容。plupload使用“multipart/form-data”这种表单上传文件,其中每一个分块会发出一次请求,表单中有两个字段,分别是“chunk”和“chunks”,其中“chunk”是当前正在处理的文件分块的序号(从0开始计数),而“chunks”则是文件的分块总数。

(1).net

  asp.net MVC【有bug】

/// <summary>
/// 文件上传
/// </summary>
/// <returns></returns>
public JsonResult plupload(string name)
{ string msg = string.Empty;string strchunk = Request["chunk"];string strchunks = Request["chunks"];int chunk = 0;int chunks = 0;int.TryParse(strchunk, out chunk);int.TryParse(strchunks, out chunks);foreach (string upload in Request.Files){if (upload != null && upload.Trim() != ""){string path = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";if (!Directory.Exists(path)){Directory.CreateDirectory(path);}System.Web.HttpPostedFileBase postedFile = Request.Files[upload];string filename1 = Path.GetFileName(postedFile.FileName);string filename = name;string newFileName = filename;if (chunks>1){newFileName = chunk + "_" + filename;}string fileNamePath = path + newFileName;postedFile.SaveAs(fileNamePath);if (chunks>1 && chunk + 1 == chunks){using (FileStream fsw = new FileStream(path + filename, FileMode.Create, FileAccess.Write)){BinaryWriter bw = new BinaryWriter(fsw);// 遍历文件合并 for (int i = 0; i < chunks; i++){bw.Write(System.IO.File.ReadAllBytes(path + i.ToString() + "_" + filename));bw.Flush();}}}}} return Json(new { jsonrpc = "2.0", result = "", id = "id" });}

(2)servlet

config.properties:

uploadPath=/upload/images

package cn.getword.servlet;import com.alibaba.fastjson.JSON;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;@WebServlet(name = "FileUploadServlet", urlPatterns = {"/upload/plupload.do"})
public class FileUploadServlet extends HttpServlet {String uploadPath;private static final ResourceBundle bundle = ResourceBundle.getBundle( "config" );protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setCharacterEncoding( "UTF-8" );Integer chunk = null; /* 分割块数 */Integer chunks = null; /* 总分割数 */String tempFileName = null; /* 临时文件名 */String newFileName = null; /* 最后合并后的新文件名 */BufferedOutputStream    outputStream    = null;/* System.out.println(FileUtils.getTempDirectoryPath()); */uploadPath = request.getServletContext().getRealPath( bundle.getString( "uploadPath" ) );File up = new File( uploadPath );if ( !up.exists() ){up.mkdir();}if ( ServletFileUpload.isMultipartContent( request ) ){try {DiskFileItemFactory factory = new DiskFileItemFactory();factory.setSizeThreshold( 1024 );/* factory.setRepository(new File(repositoryPath));// 设置临时目录 */ServletFileUpload upload = new ServletFileUpload( factory );upload.setHeaderEncoding( "UTF-8" );/* upload.setSizeMax(5 * 1024 * 1024);// 设置附件最大大小,超过这个大小上传会不成功 */List<FileItem> items = upload.parseRequest( request );for ( FileItem item : items ){if ( item.isFormField() ) /* 是文本域 */{if ( item.getFieldName().equals( "name" ) ){tempFileName = item.getString();/* System.out.println("临时文件名:" + tempFileName); */} else if ( item.getFieldName().equals( "chunk" ) ){chunk = Integer.parseInt( item.getString() );/* System.out.println("当前文件块:" + (chunk + 1)); */} else if ( item.getFieldName().equals( "chunks" ) ){chunks = Integer.parseInt( item.getString() );/* System.out.println("文件总分块:" + chunks); */}} else { /* 如果是文件类型 */if ( tempFileName != null ){String chunkName = tempFileName;if ( chunk != null ){chunkName = chunk + "_" + tempFileName;}File savedFile = new File( uploadPath, chunkName );item.write( savedFile );}}}// UUID+'.'+后缀名newFileName = UUID.randomUUID().toString().replace( "-", "" ).concat( "." ).concat( FilenameUtils.getExtension( tempFileName ) );if ( chunk != null && chunk + 1 == chunks ){outputStream = new BufferedOutputStream(new FileOutputStream( new File( uploadPath, newFileName ) ) );/* 遍历文件合并 */for ( int i = 0; i < chunks; i++ ){File tempFile = new File( uploadPath, i + "_" + tempFileName );byte[] bytes = FileUtils.readFileToByteArray( tempFile );outputStream.write( bytes );outputStream.flush();tempFile.delete();}outputStream.flush();}Map<String, Object> m = new HashMap<String, Object>();m.put( "status", true );m.put( "fileUrl", bundle.getString( "uploadPath" ) + "/"+ newFileName );response.getWriter().write( JSON.toJSONString( m ) );} catch ( FileUploadException e ) {e.printStackTrace();Map<String, Object> m = new HashMap<String, Object>();m.put( "status", false );response.getWriter().write( JSON.toJSONString( m ) );} catch ( Exception e ) {e.printStackTrace();Map<String, Object> m = new HashMap<String, Object>();m.put( "status", false );response.getWriter().write( JSON.toJSONString( m ) );} finally {try {if ( outputStream != null )outputStream.close();} catch ( IOException e ) {e.printStackTrace();}}}}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.getRequestDispatcher("/WEB-INF/views/student/uploadFile.jsp").forward(request, response);}
}

servlet

  注意:

> request.getServletContext().getRealPath(virtualPath);  将虚拟路径转化成物理路径。
> ResourceBundle bundle = ResourceBundle.getBundle( "config" );读取src下的config.properties文件。例如:file.config对应的文件为file包下config.properties文件

转载于:https://www.cnblogs.com/zhuxiang1633/p/8182118.html

plupload2.1.2文件合并相关推荐

  1. Java实现文件分割和文件合并实例

    文件切割和文件合并这个问题困扰了我有一段时间了(超过一天没做粗来). 找了好多博客,本来想转载一个来的 结果找不到了.很无奈. 只好自己贴代码上了. 当然我会尽力好好写注释的. 文件切割器: impo ...

  2. linux下将多个文件去除文件头合并_shell命令实现当前目录下多个文件合并为一个文件的方法...

    当前目录下多个文件合并为一个文件 1.将多个文件合并为一个文件没有添加换行符 find ./ -name "iptv_authenticate_201801*" | xargs c ...

  3. 将多个PDF文件合并/转换为一个PDF

    如何将多个PDF文件合并/转换为一个大PDF文件? 我尝试了以下操作,但是目标文件的内容不符合预期: convert file1.pdf file2.pdf merged.pdf 我需要一个非常简单/ ...

  4. 大数据教程(10.6)自定义inputFormat(小文件合并)

    2019独角兽企业重金招聘Python工程师标准>>> 上一篇文章分析了运营商流量日志解析增强的实现,至此,mapreduce的组件中除了inputFormat全都自定义写过了!博主 ...

  5. 两个音轨合并_webm格式视频文件合并+weba音频文件无损合并

    WebM 格式,其实是以 Matroska(就是我们熟知的 MKV)容器格式为基础开发的新容器格式,里面包括了 VP8 视频和 Ogg Vorbis 音轨.简单点说webm格式文件可以是视频,也可以是 ...

  6. python 整合excel_Python将多个excel文件合并为一个文件

    # -*- coding: utf-8 -*- #将多个Excel文件合并成一个 import xlrd import xlsxwriter #打开一个excel文件 def open_xls(fil ...

  7. 如何利用python整合excel_Python将多个excel文件合并为一个文件

    Python将多个excel文件合并为一个文件 这篇文章主要为大家详细介绍了Python将多个excel文件合并为一个文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 思路 利用python x ...

  8. python处理多个excel文件-Python将多个excel文件合并为一个文件

    利用Python,将多个excel文件合并为一个文件 思路 利用python xlrd包读取excle文件,然后将文件内容存入一个列表中,再利用xlsxwriter将内容写入到一个新的excel文件中 ...

  9. python处理多个excel文件-python多个excel文件合并成一个sheet

    运营人员需要历年的订单数据,这就需要把多个文件夹下面的excel文件合并到一个sheet中,之前的解决的办法是用VBA把多个excel文件合并成一个表的多个sheet,再把多个sheet合并成一个sh ...

  10. excel函数去重_Java 嵌入 SPL 轻松实现 Excel 文件合并

    大多数JAVA程序猿都选择使用POI或者HSSFWorkbook等第三方类库来实现Excel自动化合并,这样一来不仅需要噼里啪啦的敲好多代码,费事费力,而且用起来灵活度也不高,对Excel的格式要求也 ...

最新文章

  1. 如何修改select的样式
  2. WCF部署:让IIS有权限访问证书文件
  3. 理解事件捕获。在限制范围内拖拽div+吸附+事件捕获
  4. 内核模块相关命令:lsmod,depmod,modprob,modinfo,insmod,rmmod
  5. 正弦定理和余弦定理_苏州市高一数学(正弦定理与余弦定理)线上教育学案
  6. 毫米波雷达和车联网在未来无人驾驶中的应用和比较
  7. .NET Core TDD前传: 编写易于测试的代码 -- 缝
  8. 【Spring】Spring boot的ApplicationContextAware 实现获取service
  9. drop table 、delete table和truncate table的区别
  10. XSS、CSRF与验证码等等
  11. 如何在macOS Big Sur中使用快速用户切换?
  12. 前端学习笔记--百度2010校园招聘题目
  13. 惊呆了!不改一行 Java 代码竟然就能轻松解决敏感信息加解密|原创
  14. 使用mysql 函数 IFNULL 解决某些字段为null
  15. 如何根据芯片手册时序图编写驱动程序
  16. BZOJ 4565 字符合并 (区间状压dp)
  17. getch方法_c语言中getch的用法
  18. [转]一个商业计划书模板
  19. 在 Mac/win7 下上使用 Vagrant 打造本地开发环境
  20. 看完南京前11年房价,我哭了!今年实在太特么狠了

热门文章

  1. 基于扩展性考虑,不同场景选择的不同方案
  2. Android客户端和服务器端数据交互的第三种方法
  3. 构造函数与一般函数的区别
  4. docker安装mySQL 8
  5. 教你轻松搞定Vue事件总线(EventBus)
  6. C++ 预编译头文件stdafx.h
  7. 用python画明星_Python也能成为毕加索?我用Python给小姐姐画了幅油画
  8. python中正则表达式的用法_详解Python中的正则表达式的用法
  9. 华为手机耳机sws音效是什么_耳机sws音效是什么 华为手机耳机sws音效是什么
  10. mysql判断时间是否在某个区间_如何正确理解 RT 并监控 MySQL 的响应时间