├── .gitignore ├── java-upload ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── devsai │ │ │ ├── ctrl │ │ │ ├── MainCtrl.class │ │ │ ├── MyHiddenFileFilter.java │ │ │ ├── CommonResult.java │ │ │ ├── MainCtrl.java │ │ │ └── UploadController.java │ │ │ ├── test │ │ │ ├── MyMd5.java │ │ │ └── MyProtoBuf.java │ │ │ ├── ws │ │ │ ├── MyWSThread.java │ │ │ ├── EventServlet.java │ │ │ ├── EventServer.java │ │ │ ├── MyServlet.java │ │ │ ├── EventSocket.java │ │ │ └── StockServiceWebSocket.java │ │ │ ├── view │ │ │ ├── ViewInitView.java │ │ │ └── MultiUrlViewResolver.java │ │ │ ├── filter │ │ │ └── MyFilter.java │ │ │ ├── model │ │ │ ├── UploadFile.java │ │ │ └── UploadMsg.java │ │ │ └── service │ │ │ └── UploadService.java │ │ └── webapp │ │ ├── static │ │ ├── proto │ │ │ └── upload.proto │ │ ├── index.css │ │ ├── index.html │ │ ├── uploadMethod │ │ │ ├── formUpload.html │ │ │ ├── iframeUpload.html │ │ │ ├── chunkUpload.html │ │ │ ├── wsUpload.html │ │ │ └── ajaxUpload.html │ │ ├── fileList.html │ │ ├── js │ │ │ └── chunk.js │ │ └── demo.html │ │ └── WEB-INF │ │ ├── jsp │ │ └── hello.jsp │ │ ├── log4j.xml │ │ ├── web.xml │ │ ├── applicationContext.xml │ │ └── UploadWeb-servlet.xml ├── uploadserver.iml └── pom.xml ├── node-upload ├── static │ ├── proto │ │ └── upload.proto │ ├── style.css │ ├── index.html │ ├── js │ │ └── chunk.js │ └── uploadMethod │ │ ├── corsUpload.html │ │ ├── iframeUpload.html │ │ ├── chunkUpload.html │ │ ├── ajaxUpload.html │ │ └── wsUpload.html ├── package.json ├── ws_server.js └── server.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | **/node_module 2 | node-upload/node_modules 3 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/ctrl/MainCtrl.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangxiangsai/web-upload-demo/HEAD/java-upload/src/main/java/com/devsai/ctrl/MainCtrl.class -------------------------------------------------------------------------------- /node-upload/static/proto/upload.proto: -------------------------------------------------------------------------------- 1 | message Upload { 2 | required string filename = 1; 3 | required int32 currchunk = 2; 4 | required int32 chunks = 3; 5 | required int32 uid = 4; 6 | required bytes upload_file = 5; 7 | } 8 | -------------------------------------------------------------------------------- /java-upload/src/main/webapp/static/proto/upload.proto: -------------------------------------------------------------------------------- 1 | message Upload { 2 | required string filename = 1; 3 | required int32 currchunk = 2; 4 | required int32 chunks = 3; 5 | required int32 uid = 4; 6 | required bytes upload_file = 5; 7 | } 8 | -------------------------------------------------------------------------------- /java-upload/src/main/webapp/WEB-INF/jsp/hello.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html; charset=UTF-8" %> 2 | 3 | 4 | Hello World 5 | 6 | 7 |

${message}

8 |

当前时间:${currdate}

9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/test/MyMd5.java: -------------------------------------------------------------------------------- 1 | package com.devsai.test; 2 | 3 | import sun.security.provider.MD5; 4 | 5 | /** 6 | * Created by huangxiangsai on 16/6/17. 7 | */ 8 | public class MyMd5 { 9 | 10 | public static void main(String[] argv){ 11 | MD5 md5 = new MD5(); 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/ctrl/MyHiddenFileFilter.java: -------------------------------------------------------------------------------- 1 | package com.devsai.ctrl; 2 | 3 | import org.apache.commons.io.filefilter.AbstractFileFilter; 4 | import org.apache.commons.io.filefilter.HiddenFileFilter; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * Created by huangxiangsai on 16/6/30. 10 | */ 11 | public class MyHiddenFileFilter extends HiddenFileFilter implements Serializable { 12 | } 13 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/ws/MyWSThread.java: -------------------------------------------------------------------------------- 1 | //package com.devsai.ws; 2 | // 3 | ///** 4 | // * Created by huangxiangsai on 16/7/6. 5 | // */ 6 | //public class MyWSThread implements Runnable{ 7 | // 8 | // public MyWSThread(){ 9 | // EventServer es= new EventServer(); 10 | // es.createServer(); 11 | // System.out.println("create websocket server........"); 12 | // } 13 | // public void run() { 14 | // 15 | // } 16 | //} 17 | -------------------------------------------------------------------------------- /node-upload/static/style.css: -------------------------------------------------------------------------------- 1 | iframe{ 2 | display:block; 3 | } 4 | 5 | 6 | .chooseFile{ 7 | position: relative; 8 | background-color: white; 9 | overflow: hidden; 10 | } 11 | 12 | .chooseFile input{ 13 | width: 161px; 14 | opacity: 0; 15 | position: absolute; 16 | display: inline-block; 17 | float: left; 18 | left: -80px; 19 | top: 0; 20 | } 21 | 22 | .mgl-30{ 23 | margin-left : 30px; 24 | } 25 | 26 | 27 | .list-group.js-list{ 28 | display:inline-block; 29 | width:49%; 30 | } -------------------------------------------------------------------------------- /java-upload/src/main/webapp/static/index.css: -------------------------------------------------------------------------------- 1 | iframe{ 2 | display:block; 3 | } 4 | 5 | 6 | .chooseFile{ 7 | position: relative; 8 | background-color: white; 9 | overflow: hidden; 10 | } 11 | 12 | .chooseFile input{ 13 | width: 161px; 14 | opacity: 0; 15 | position: absolute; 16 | display: inline-block; 17 | float: left; 18 | left: -80px; 19 | top: 0; 20 | } 21 | 22 | .mgl-30{ 23 | margin-left : 30px; 24 | } 25 | 26 | 27 | .list-group.js-list{ 28 | display:inline-block; 29 | width:49%; 30 | } -------------------------------------------------------------------------------- /node-upload/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-upload-demo", 3 | "version": "1.0.0", 4 | "description": "web uplaod file demo for node", 5 | "main": "server.js", 6 | "scripts": { 7 | "start": "node server.js & node ws_server.js" 8 | }, 9 | "keywords": [ 10 | "upload", 11 | "ajax" 12 | ], 13 | "author": "SAI", 14 | "license": "ISC", 15 | "devDependencies": { 16 | "content-type-mime": "^1.0.1", 17 | "formidable": "^1.0.17", 18 | "json-stable-stringify": "^1.0.1", 19 | "koa": "^1.2.4", 20 | "koa-better-body": "^3.0.2", 21 | "koa-cors": "^0.0.16", 22 | "koa-router": "^5.4.0", 23 | "koa-static": "^2.0.0", 24 | "protobufjs": "^5.0.1", 25 | "ws": "^1.1.1" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/view/ViewInitView.java: -------------------------------------------------------------------------------- 1 | package com.devsai.view; 2 | 3 | import org.springframework.web.servlet.view.AbstractTemplateView; 4 | 5 | import javax.servlet.http.HttpServletRequest; 6 | import javax.servlet.http.HttpServletResponse; 7 | import java.util.Map; 8 | 9 | /** 10 | * Created by huangxiangsai on 16/6/21. 11 | */ 12 | public class ViewInitView extends AbstractTemplateView { 13 | @Override 14 | protected void renderMergedTemplateModel(Map map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { 15 | 16 | } 17 | 18 | protected void exposeHelpers(Map model, 19 | HttpServletRequest request) throws Exception { 20 | model.put("base", request.getContextPath()); 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/ws/EventServlet.java: -------------------------------------------------------------------------------- 1 | package com.devsai.ws; 2 | 3 | /** 4 | * Created by huangxiangsai on 16/7/1. 5 | */ 6 | import org.eclipse.jetty.websocket.servlet.WebSocketServlet; 7 | import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; 8 | 9 | @SuppressWarnings("serial") 10 | public class EventServlet extends WebSocketServlet 11 | { 12 | private static final Integer MAX_MESSAGE_SIZE = 920092544; 13 | 14 | @Override 15 | public void configure(WebSocketServletFactory factory) 16 | { 17 | factory.getPolicy().setMaxBinaryMessageSize(MAX_MESSAGE_SIZE); 18 | factory.getPolicy().setMaxBinaryMessageBufferSize(MAX_MESSAGE_SIZE); 19 | factory.getPolicy().setMaxTextMessageBufferSize(MAX_MESSAGE_SIZE); 20 | factory.getPolicy().setMaxTextMessageSize(MAX_MESSAGE_SIZE); 21 | factory.register(EventSocket.class); 22 | } 23 | } -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/filter/MyFilter.java: -------------------------------------------------------------------------------- 1 | package com.devsai.filter; 2 | 3 | import org.springframework.web.filter.OncePerRequestFilter; 4 | 5 | import javax.servlet.FilterChain; 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | import java.io.IOException; 10 | 11 | /** 12 | * Created by huangxiangsai on 16/11/8. 13 | */ 14 | public class MyFilter extends OncePerRequestFilter{ 15 | @Override 16 | protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException { 17 | 18 | // if (httpServletRequest.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(httpServletRequest.getMethod())) { 19 | httpServletResponse.addHeader("Access-Control-Allow-Origin","*"); 20 | 21 | // } 22 | System.out.println("myFilter..."); 23 | filterChain.doFilter(httpServletRequest,httpServletResponse); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # web上传 2 | 3 | web上传在网站开发中经常会用到,web上传在很早前就有,可通过表单提交实现上传, 4 | 5 | 后来flash插件大举进入网站,使用flash实现,可以实现强大的上传功能 6 | 7 | 在后来有了HTML5,ajax支持了上传文件,不需要flash也能实现强大的上传。 8 | 9 | 本栗子只做了简单的上传Demo,想了解web上传相关可以查看[聊聊web上传](http://huangxiangsai.github.io/2016/11/08/talk-web-upload/) 10 | 11 | ## 提供了两个版本的上传服务 12 | 13 | node版web上传 、 java版web上传 14 | 15 | ## 如何使用demo 16 | 17 | ### node版web上传 18 | 19 | * 进入node-upload目录 20 | * 使用npm i 安装依赖(当然已经配置了cnpm,也可以使用cnpm) 21 | * 执行命令npm start启动服务(将会启动两个服务:web服务,websocket服务) 22 | * 打开浏览器访问静态文件127.0.0.1:8083/index.html 23 | 24 | ### java版web上传 25 | 26 | * 导入到IDE 27 | * 加载maven依赖 28 | * 启动jetty(当然也可以其他web服务) 29 | * 打开浏览器访问静态文件127.0.0.1:8888/static/index.html 30 | * 执行com.devsai.ws.EventServer.java的main方法启动websocket服务 31 | 32 | 33 | ### 跨域资源共享(用于测试上传跨域的简单请求及预检测请求) 34 | 35 | 此部分内容为你所不知道的跨域资源共享(CORS)的DEMO 36 | 37 | 以node版为例, 38 | 在node版服务端中添加了跨域资源共享的支持,并添加了**corsUpload**路由接口 39 | 40 | 通过访问`http://127.0.0.1:8083/index.html` >> 跨域上传 进入页面 41 | 42 | html页面路径为**/static/uploadMethod/corsUpload.html** 43 | 44 | 打开控制台,上传文件,观察上传的请求情况, 45 | 46 | 也可修改html页面中JS,根据博文中所说的,进行修改,再次上传,观察上传请求情况。 47 | 48 | -------------------------------------------------------------------------------- /java-upload/src/main/webapp/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 |

Web Upload File 详解

12 |
13 |
14 |
web上传的几种方法
15 | 27 |
28 | 29 | 30 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/model/UploadFile.java: -------------------------------------------------------------------------------- 1 | package com.devsai.model; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Created by huangxiangsai on 16/5/22. 7 | */ 8 | public class UploadFile implements Serializable{ 9 | 10 | 11 | 12 | private String name; 13 | 14 | private Long size; 15 | 16 | private String type; 17 | 18 | private String path; 19 | 20 | private byte[] content; 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | public Long getSize() { 31 | return size; 32 | } 33 | 34 | public void setSize(Long size) { 35 | this.size = size; 36 | } 37 | 38 | public String getType() { 39 | return type; 40 | } 41 | 42 | public void setType(String type) { 43 | this.type = type; 44 | } 45 | 46 | public String getPath() { 47 | return path; 48 | } 49 | 50 | public void setPath(String path) { 51 | this.path = path; 52 | } 53 | 54 | public byte[] getContent() { 55 | return content; 56 | } 57 | 58 | public void setContent(byte[] content) { 59 | this.content = content; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /node-upload/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 |

Web Upload File 详解

12 |
13 |
14 |
web上传的几种方法
15 | 28 |
29 | 30 | 31 | 32 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/test/MyProtoBuf.java: -------------------------------------------------------------------------------- 1 | package com.devsai.test; 2 | 3 | import com.devsai.model.UploadMsg; 4 | import com.google.protobuf.ByteString; 5 | 6 | /** 7 | * Created by huangxiangsai on 16/7/8. 8 | */ 9 | public class MyProtoBuf { 10 | 11 | public static void main(String[] args) { 12 | 13 | // creating the cat 14 | UploadMsg.Upload uploadMsg= UploadMsg.Upload.newBuilder() 15 | .setFilename("") 16 | .setChunks(10) 17 | .setUid(1) 18 | .setCurrchunk(1) 19 | // .setFilenameBytes(new byte[4]) 20 | .build(); 21 | 22 | ByteString uploadFile = uploadMsg.getUploadFile(); 23 | byte[] content = new byte[uploadFile.size()]; 24 | uploadFile.copyTo(content,0); 25 | 26 | 27 | // try { 28 | // // write 29 | // FileOutputStream output = new FileOutputStream("cat.ser"); 30 | // pusheen.writeTo(output); 31 | // output.close(); 32 | // 33 | // // read 34 | // Cat catFromFile = Cat.parseFrom(new FileInputStream("cat.ser")); 35 | // System.out.println(catFromFile); 36 | // 37 | // } catch (IOException e) { 38 | // e.printStackTrace(); 39 | // } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/ws/EventServer.java: -------------------------------------------------------------------------------- 1 | package com.devsai.ws; 2 | 3 | /** 4 | * Created by huangxiangsai on 16/7/1. 5 | */ 6 | import org.eclipse.jetty.server.Server; 7 | import org.eclipse.jetty.server.ServerConnector; 8 | import org.eclipse.jetty.servlet.ServletContextHandler; 9 | import org.eclipse.jetty.servlet.ServletHolder; 10 | 11 | public class EventServer 12 | { 13 | 14 | private Server server; 15 | 16 | 17 | public static void main(String[] args) 18 | { 19 | Server server = new Server(); 20 | ServerConnector connector = new ServerConnector(server); 21 | connector.setPort(8081); 22 | server.addConnector(connector); 23 | 24 | // Setup the basic application "context" for this application at "/" 25 | // This is also known as the handler tree (in jetty speak) 26 | ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); 27 | context.setContextPath("/"); 28 | server.setHandler(context); 29 | // Add a websocket to a specific path spec 30 | ServletHolder holderEvents = new ServletHolder("ws-events", EventServlet.class); 31 | 32 | context.addServlet(holderEvents, "/upload/*"); 33 | 34 | // holderEvents. 35 | 36 | try 37 | { 38 | server.start(); 39 | server.dump(System.err); 40 | server.join(); 41 | } 42 | catch (Throwable t) 43 | { 44 | t.printStackTrace(System.err); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/ws/MyServlet.java: -------------------------------------------------------------------------------- 1 | package com.devsai.ws; 2 | 3 | /** 4 | * Created by huangxiangsai on 16/7/6. 5 | */ 6 | import java.io.IOException; 7 | import javax.servlet.ServletException; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | import javax.servlet.annotation.WebServlet; 11 | import org.eclipse.jetty.websocket.servlet.WebSocketServlet; 12 | import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; 13 | 14 | @WebServlet(name = "WebSocket Servlet", urlPatterns = { "/wsexample" }) 15 | public class MyServlet extends WebSocketServlet { 16 | 17 | private static final Integer MAX_MESSAGE_SIZE = 10000000; 18 | 19 | @Override 20 | public void doGet(HttpServletRequest request, 21 | HttpServletResponse response) throws ServletException, IOException { 22 | response.getWriter().println("HTTP GET method not implemented."); 23 | } 24 | 25 | @Override 26 | public void configure(WebSocketServletFactory factory) { 27 | factory.getPolicy().setIdleTimeout(10000); 28 | factory.getPolicy().setMaxBinaryMessageSize(MAX_MESSAGE_SIZE); 29 | factory.getPolicy().setMaxBinaryMessageBufferSize(MAX_MESSAGE_SIZE); 30 | factory.getPolicy().setInputBufferSize(MAX_MESSAGE_SIZE); 31 | factory.getPolicy().setMaxTextMessageBufferSize(MAX_MESSAGE_SIZE); 32 | factory.getPolicy().setMaxTextMessageSize(MAX_MESSAGE_SIZE); 33 | factory.register(StockServiceWebSocket.class); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/ctrl/CommonResult.java: -------------------------------------------------------------------------------- 1 | package com.devsai.ctrl; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | /** 7 | * Created by huangxiangsai on 16/5/21. 8 | */ 9 | public class CommonResult { 10 | 11 | private int code ; 12 | private String message; 13 | private List> data; 14 | 15 | public int getCode() { 16 | return code; 17 | } 18 | 19 | public void setCode(int code) { 20 | this.code = code; 21 | } 22 | 23 | public String getMessage() { 24 | return message; 25 | } 26 | 27 | public void setMessage(String message) { 28 | this.message = message; 29 | } 30 | 31 | public List> getData() { 32 | return data; 33 | } 34 | 35 | public void setData(List> data) { 36 | this.data = data; 37 | } 38 | 39 | @Override 40 | public boolean equals(Object o) { 41 | if (this == o) return true; 42 | if (o == null || getClass() != o.getClass()) return false; 43 | 44 | CommonResult that = (CommonResult) o; 45 | 46 | if (code != that.code) return false; 47 | if (!message.equals(that.message)) return false; 48 | return !(data != null ? !data.equals(that.data) : that.data != null); 49 | 50 | } 51 | 52 | @Override 53 | public int hashCode() { 54 | int result = code; 55 | result = 31 * result + message.hashCode(); 56 | result = 31 * result + (data != null ? data.hashCode() : 0); 57 | return result; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /node-upload/ws_server.js: -------------------------------------------------------------------------------- 1 | /** 2 | * node版websocket上传Demo 3 | * 代码粗陋,只能用于学习 4 | * @Author SAI 5 | * @DateTime 2016-11-12T19:43:12+0800 6 | */ 7 | var fs = require("fs") 8 | , path = require("path") 9 | , server = require('http').createServer() 10 | , WebSocketServer = require('ws').Server 11 | , wss = new WebSocketServer({ server: server,path : '/upload' }) 12 | , port = 8081 13 | , ProtoBuf = require("protobufjs"); 14 | 15 | 16 | var UploadManage = function () { 17 | 18 | } 19 | 20 | UploadManage.prototype.saveChunk = function(params,_Bufferfile) { 21 | try{ 22 | var id = params.id,chunk = params.chunk,name = params.name; 23 | var _path = __dirname+'/uploadFile/'+id+'/'; 24 | if(!fs.existsSync(_path)){ 25 | fs.mkdirSync(_path); 26 | } 27 | fs.writeFileSync(__dirname+'/uploadFile/'+id+'/'+name+'_'+chunk, _Bufferfile); 28 | var result = {code : 200} 29 | }catch(e){ 30 | return {code : -1} 31 | } 32 | return result; 33 | 34 | } 35 | 36 | // Initialize and load upload.proto file 37 | var builder = ProtoBuf.loadProtoFile(path.join(__dirname, "static","proto", "upload.proto")); 38 | var Upload = builder.build("Upload"); 39 | 40 | wss.on('connection', function connection(ws) { 41 | var uploadManage = new UploadManage(); 42 | 43 | ws.on('message', function incoming(data,flags) { 44 | console.log(arguments); 45 | if (flags.binary) { 46 | var upload = Upload.decode(data); 47 | console.log(upload.filename,upload.uid); 48 | uploadManage.saveChunk({id : upload.uid,chunk: upload.currchunk,name : upload.filename},upload.upload_file); 49 | } 50 | }); 51 | 52 | }); 53 | 54 | server.listen(port); 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /java-upload/src/main/webapp/WEB-INF/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 20 | 21 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/ws/EventSocket.java: -------------------------------------------------------------------------------- 1 | package com.devsai.ws; 2 | 3 | /** 4 | * Created by huangxiangsai on 16/7/1. 5 | */ 6 | import com.devsai.service.UploadService; 7 | import org.eclipse.jetty.websocket.api.Session; 8 | import org.eclipse.jetty.websocket.api.WebSocketAdapter; 9 | 10 | import java.io.IOException; 11 | 12 | public class EventSocket extends WebSocketAdapter 13 | { 14 | 15 | private UploadService us = new UploadService(); 16 | 17 | 18 | @Override 19 | public void onWebSocketConnect(Session sess) 20 | { 21 | super.onWebSocketConnect(sess); 22 | System.out.println("Socket Connected: " + sess); 23 | } 24 | 25 | @Override 26 | public void onWebSocketBinary(byte[] payload, int offset, int len) { 27 | super.onWebSocketBinary(payload, offset, len); 28 | System.out.println("payload"); 29 | System.out.println(payload); 30 | System.out.println(payload.length); 31 | us.saveFile(payload); 32 | } 33 | 34 | @Override 35 | public void onWebSocketText(String message) 36 | { 37 | super.onWebSocketText(message); 38 | System.out.println("Received TEXT message: " + message); 39 | try { 40 | this.getRemote().sendString(""); 41 | } catch (IOException e) { 42 | e.printStackTrace(); 43 | } 44 | } 45 | 46 | @Override 47 | public void onWebSocketClose(int statusCode, String reason) 48 | { 49 | super.onWebSocketClose(statusCode,reason); 50 | this.getSession().close(); 51 | System.out.println("Socket Closed: [" + statusCode + "] " + reason); 52 | } 53 | 54 | @Override 55 | public void onWebSocketError(Throwable cause) 56 | { 57 | super.onWebSocketError(cause); 58 | cause.printStackTrace(System.err); 59 | } 60 | 61 | 62 | } -------------------------------------------------------------------------------- /java-upload/src/main/webapp/static/uploadMethod/formUpload.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 表单上传 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 |
普通表单上传文件
15 |
16 | 17 | 28 | 29 |
30 |
31 | 32 | 48 | 49 | -------------------------------------------------------------------------------- /java-upload/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 24 | 29 | 30 | Spring MVC Application 31 | 32 | 33 | UploadWeb 34 | 35 | org.springframework.web.servlet.DispatcherServlet 36 | 37 | 1 38 | 39 | 40 | 41 | UploadWeb 42 | / 43 | 44 | 45 | 46 | MyFilter 47 | com.devsai.filter.MyFilter 48 | 49 | 50 | MyFilter 51 | /* 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/ctrl/MainCtrl.java: -------------------------------------------------------------------------------- 1 | package com.devsai.ctrl; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.lang.reflect.Array; 6 | import java.text.SimpleDateFormat; 7 | import java.util.Calendar; 8 | import java.util.Date; 9 | import java.util.logging.SimpleFormatter; 10 | 11 | /** 12 | * Created by huangxiangsai on 16/4/19. 13 | */ 14 | public class MainCtrl { 15 | 16 | public MainCtrl(){ 17 | 18 | } 19 | 20 | 21 | public static void main(String[] args){ 22 | 23 | Calendar calendar = Calendar.getInstance(); 24 | calendar.setTime(new Date()); 25 | 26 | SimpleDateFormat sf = new SimpleDateFormat("yy-MM-dd"); 27 | Long time = System.currentTimeMillis(); 28 | // System.out.println(sf.format(time)); 29 | // File file = new File("com/"+sf.format(time)); 30 | // if(!file.exists()){ 31 | // try { 32 | // boolean success = file.createNewFile(); 33 | // System.out.println(success); 34 | // } catch (IOException e) { 35 | // e.printStackTrace(); 36 | // } 37 | // 38 | // } 39 | 40 | String name = "huangxiangsai"; 41 | StringBuffer sb = new StringBuffer("huang"); 42 | 43 | System.out.println(name.contentEquals(sb)); 44 | System.out.println(name.indexOf("xiang")); 45 | 46 | System.out.println("Array -----------------"); 47 | int[] list = new int[]{3,6,1,62,34,22,9}; 48 | for(int i = 0 ;i < list.length ; i++){ 49 | int x = list[i]; 50 | for(int j = i+1 ; j < list.length ; j++){ 51 | int y = list[j]; 52 | if(x > y){ 53 | int temp = x; 54 | list[i] = y; 55 | list[j] = temp; 56 | x= y; 57 | 58 | } 59 | } 60 | } 61 | 62 | for(int i = 0 ; i < list.length ; i++){ 63 | System.out.println(list[i]); 64 | } 65 | 66 | 67 | 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /java-upload/src/main/webapp/WEB-INF/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 28 | 29 | 30 | 31 | 32 | 33 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/view/MultiUrlViewResolver.java: -------------------------------------------------------------------------------- 1 | package com.devsai.view; 2 | 3 | /** 4 | * Created by huangxiangsai on 16/6/21. 5 | */ 6 | 7 | import java.util.Locale; 8 | import java.util.Map; 9 | import java.util.regex.Matcher; 10 | import java.util.regex.Pattern; 11 | 12 | import javax.annotation.PostConstruct; 13 | 14 | import org.springframework.web.servlet.View; 15 | import org.springframework.web.servlet.ViewResolver; 16 | import org.springframework.web.servlet.view.AbstractCachingViewResolver; 17 | 18 | public class MultiUrlViewResolver extends AbstractCachingViewResolver { 19 | 20 | private Map resolvers; 21 | 22 | private ThreadLocal loadview=new ThreadLocal(); 23 | 24 | public boolean isLoading(){ 25 | try{ 26 | return this.loadview.get(); 27 | }catch(Exception e){ 28 | return false; 29 | } 30 | } 31 | 32 | @PostConstruct 33 | public void init(){ 34 | this.loadview.set(false); 35 | } 36 | 37 | public Map getResolvers() { 38 | return resolvers; 39 | } 40 | 41 | public void setResolvers(Map resolvers) { 42 | this.resolvers = resolvers; 43 | } 44 | 45 | @Override 46 | protected View loadView(String viewName, Locale locale) throws Exception { 47 | 48 | ViewResolverResult res=findViewResolver(viewName); 49 | 50 | this.loadview.set(true); 51 | View view= res.getResolver().resolveViewName(res.getViewName(), locale); 52 | this.loadview.set(false); 53 | return view; 54 | } 55 | 56 | private Pattern viewType=Pattern.compile("^(\\w+):(.*)"); 57 | 58 | private ViewResolverResult findViewResolver(String viewName){ 59 | Matcher m=viewType.matcher(viewName); 60 | if(m.matches()){ 61 | String curName=m.group(2); 62 | ViewResolver vr=this.resolvers.get(m.group(1)); 63 | if(vr==null){ 64 | vr=this.resolvers.get("jsp"); 65 | } 66 | return new ViewResolverResult(curName, vr); 67 | } 68 | return new ViewResolverResult(viewName, resolvers.get("jsp")); 69 | } 70 | 71 | private static final class ViewResolverResult{ 72 | private String viewName; 73 | 74 | private ViewResolver resolver; 75 | 76 | public ViewResolverResult(String viewName,ViewResolver resolver) { 77 | this.viewName=viewName; 78 | this.resolver=resolver; 79 | } 80 | 81 | public String getViewName() { 82 | return viewName; 83 | } 84 | public ViewResolver getResolver() { 85 | return resolver; 86 | } 87 | 88 | @Override 89 | public String toString() { 90 | return this.viewName; 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /java-upload/src/main/webapp/WEB-INF/UploadWeb-servlet.xml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | com.devsai.view.ViewInitView 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 51 | 52 | 53 | 54 | 55 | 56 | text/plain;charset=UTF-8 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /java-upload/src/main/webapp/static/fileList.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ajax上传 6 | 7 | 8 | 9 | 10 | 11 |
12 |
已上传的所有文件
13 |
14 |
15 | 16 |
17 |
18 |
19 | 20 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/service/UploadService.java: -------------------------------------------------------------------------------- 1 | package com.devsai.service; 2 | 3 | import com.devsai.model.UploadFile; 4 | import com.devsai.model.UploadMsg; 5 | import com.google.protobuf.InvalidProtocolBufferException; 6 | 7 | import javax.servlet.Servlet; 8 | import java.io.File; 9 | import java.io.FileOutputStream; 10 | import java.io.IOException; 11 | import java.text.SimpleDateFormat; 12 | import java.util.Calendar; 13 | import java.util.Date; 14 | 15 | /** 16 | * Created by huangxiangsai on 16/5/22. 17 | */ 18 | public class UploadService { 19 | private static String tempFilePath = System.getProperty("user.dir")+"/src/main/webapp/static/tempFiles/"; 20 | 21 | 22 | public UploadFile saveUploadFile(UploadFile uploadFile){ 23 | 24 | 25 | 26 | UploadFile uf = new UploadFile(); 27 | return uf; 28 | } 29 | 30 | 31 | public void saveFile(byte[] res){ 32 | try { 33 | UploadMsg.Upload upload= UploadMsg.Upload.parseFrom(res); 34 | System.out.println(upload.getChunks()); 35 | System.out.println(upload.getCurrchunk() ); 36 | System.out.println(upload.getUploadFile().size()); 37 | 38 | Integer uid = upload.getUid(); 39 | String fileName = upload.getFilename(); 40 | Integer currChunk = upload.getCurrchunk(); 41 | 42 | SimpleDateFormat sf = new SimpleDateFormat("yy-MM-dd"); 43 | Long time = System.currentTimeMillis(); 44 | String path = tempFilePath+"/"+sf.format(time)+"/"+uid; 45 | this.createDir(path); 46 | FileOutputStream out = new FileOutputStream(new File(path+"/"+fileName+".part_"+currChunk)); 47 | upload.getUploadFile().writeTo(out); 48 | out.close(); 49 | 50 | } catch (InvalidProtocolBufferException e) { 51 | e.printStackTrace(); 52 | }catch (IOException e) { 53 | e.printStackTrace(); 54 | } 55 | } 56 | 57 | public void saveFullFile(byte[] res){ 58 | try { 59 | UploadMsg.Upload upload= UploadMsg.Upload.parseFrom(res); 60 | System.out.println(upload.getChunks()); 61 | System.out.println(upload.getCurrchunk() ); 62 | 63 | Integer uid = upload.getUid(); 64 | String fileName = upload.getFilename(); 65 | Integer currChunk = upload.getCurrchunk(); 66 | 67 | SimpleDateFormat sf = new SimpleDateFormat("yy-MM-dd"); 68 | Long time = System.currentTimeMillis(); 69 | String path = tempFilePath+"/"+sf.format(time)+"/"+uid; 70 | this.createDir(path); 71 | FileOutputStream out = new FileOutputStream(new File(path+"/"+fileName+".part_"+currChunk)); 72 | upload.getUploadFile().writeTo(out); 73 | out.close(); 74 | 75 | } catch (InvalidProtocolBufferException e) { 76 | e.printStackTrace(); 77 | }catch (IOException e) { 78 | e.printStackTrace(); 79 | } 80 | } 81 | 82 | 83 | private void createDir(String path){ 84 | File dir = new File(path); 85 | if(!dir.exists()){ 86 | dir.mkdirs(); 87 | } 88 | } 89 | 90 | 91 | public static void main(String[] args){ 92 | 93 | System.out.println("sss"); 94 | } 95 | 96 | 97 | } 98 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/ws/StockServiceWebSocket.java: -------------------------------------------------------------------------------- 1 | package com.devsai.ws; 2 | 3 | /** 4 | * Created by huangxiangsai on 16/7/5. 5 | */ 6 | import com.devsai.service.UploadService; 7 | import org.eclipse.jetty.websocket.api.Session; 8 | import org.eclipse.jetty.websocket.api.annotations.*; 9 | 10 | import java.io.IOException; 11 | import java.nio.ByteBuffer; 12 | import java.util.concurrent.Executors; 13 | import java.util.concurrent.ScheduledExecutorService; 14 | import java.util.concurrent.TimeUnit; 15 | 16 | @WebSocket 17 | public class StockServiceWebSocket { 18 | 19 | private Session session; 20 | private UploadService uploadService = new UploadService(); 21 | private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); 22 | 23 | // called when the socket connection with the browser is established 24 | @OnWebSocketConnect 25 | public void handleConnect(Session session) { 26 | this.session = session; 27 | session.getPolicy().setMaxBinaryMessageBufferSize(1024*1024); 28 | session.getPolicy().setMaxBinaryMessageSize(1024*1024); 29 | 30 | } 31 | 32 | 33 | // called when the connection closed 34 | @OnWebSocketClose 35 | public void handleClose(int statusCode, String reason) { 36 | System.out.println("Connection closed with statusCode=" 37 | + statusCode + ", reason=" + reason); 38 | } 39 | 40 | // called when a message received from the browser 41 | @OnWebSocketMessage 42 | public void handleMessage(String message) { 43 | if (message.equals("start")) { 44 | send("Stock service started!"); 45 | executor.scheduleAtFixedRate(new Runnable() { 46 | public void run() { 47 | StockServiceWebSocket.this.send("ddd"); 48 | } 49 | }, 0, 5, TimeUnit.SECONDS); 50 | 51 | } else if (message.equals("stop")) { 52 | this.stop(); 53 | 54 | } 55 | } 56 | 57 | @OnWebSocketMessage 58 | public void handleMessageByByte(ByteBuffer message) { 59 | uploadService.saveFile(message.array()); 60 | this.send("success"); 61 | // if (message.equals("start")) { 62 | // send("Stock service started!"); 63 | // executor.scheduleAtFixedRate(new Runnable() { 64 | // public void run() { 65 | // StockServiceWebSocket.this.send("ddd"); 66 | // } 67 | // }, 0, 5, TimeUnit.SECONDS); 68 | // 69 | // } else if (message.equals("stop")) { 70 | // this.stop(); 71 | // 72 | // } 73 | } 74 | 75 | // called in case of an error 76 | @OnWebSocketError 77 | public void handleError(Throwable error) { 78 | error.printStackTrace(); 79 | } 80 | 81 | // sends message to browser 82 | private void send(String message) { 83 | try { 84 | if (session.isOpen()) { 85 | session.getRemote().sendString(message); 86 | } 87 | } catch (IOException e) { 88 | e.printStackTrace(); 89 | } 90 | } 91 | 92 | public void sendByte(ByteBuffer message) { 93 | try { 94 | if (session.isOpen()) { 95 | session.getRemote().sendBytes(message); 96 | } 97 | } catch (IOException e) { 98 | e.printStackTrace(); 99 | } 100 | } 101 | 102 | // closes the socket 103 | private void stop() { 104 | try { 105 | session.disconnect(); 106 | } catch (IOException e) { 107 | e.printStackTrace(); 108 | } 109 | } 110 | 111 | 112 | } -------------------------------------------------------------------------------- /java-upload/uploadserver.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /node-upload/static/js/chunk.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | function CuteFile(file, chunkSize,onprogress,cutomsSuccess) { 4 | this.uid = (new Date()).getTime(); 5 | chunkSize = chunkSize || 2 * 1024 * 1024; 6 | var pending = [], 7 | blob = file, 8 | total = blob.size, 9 | chunks = chunkSize ? Math.ceil(total / chunkSize) : 1, 10 | start = 0, 11 | index = 0, 12 | len, api; 13 | 14 | while (index < chunks) { 15 | len = Math.min(chunkSize, total - start); 16 | 17 | pending.push({ 18 | file: file, 19 | start: start, 20 | end: chunkSize ? (start + len) : total, 21 | total: total, 22 | chunks: chunks, 23 | chunk: index++, 24 | cuted: api 25 | }); 26 | start += len; 27 | } 28 | 29 | file.blocks = pending.concat(); 30 | file.remaning = pending.length; 31 | this.file = file; 32 | this.pending = pending; 33 | this.onprogress = onprogress; 34 | this.cutomsSuccess = cutomsSuccess; 35 | } 36 | 37 | CuteFile.prototype = { 38 | has: function() { 39 | return !!this.pending.length; 40 | }, 41 | 42 | shift: function() { 43 | return this.pending.shift(); 44 | }, 45 | 46 | unshift: function(block) { 47 | this.pending.unshift(block); 48 | }, 49 | stop : function(){ 50 | if(!this.xhr) return; 51 | this.xhr.upload.onprogress = null; 52 | this.xhr.onreadystatechange = null; 53 | this.xhr.abort(); 54 | this.unshift(this.currBlock); 55 | this.isStart = false; 56 | }, 57 | start : function(){ 58 | this.isStart = true; 59 | this.send(); 60 | }, 61 | switchs : function(){ 62 | if(this.isStart){ 63 | this.stop(); 64 | }else{ 65 | this.start(); 66 | } 67 | }, 68 | success : function(){ 69 | this.cutomsSuccess && this.cutomsSuccess(this.file.uid); 70 | }, 71 | send : function (){ 72 | var cuteAPI = this; 73 | if(!cuteAPI.has() ){ 74 | this.isStart = false; 75 | this.success(); 76 | return false; 77 | } 78 | var block = cuteAPI.shift(); 79 | var file = block.file; 80 | var f = file.slice(block.start, block.end); 81 | 82 | var chunks = block.chunks; 83 | var currchunk = block.chunk; 84 | var xhr = new XMLHttpRequest(); 85 | xhr.open('POST','/chunkUpload',true); 86 | var fd = new FormData(); 87 | xhr.upload.onprogress = function(data){ 88 | var result = {}; 89 | result.total = block.total; 90 | result.loaded = block.start + data.loaded; 91 | 92 | //防止进度条后退 93 | console.log(cuteAPI.currLoaded,result.loaded); 94 | if(cuteAPI.currLoaded > result.loaded){ 95 | result.loaded = this.currLoaded; 96 | }else{ 97 | cuteAPI.currLoaded = result.loaded; 98 | } 99 | //total 100 | cuteAPI.onprogress && cuteAPI.onprogress(file,result); 101 | } 102 | xhr.onreadystatechange = function() { 103 | if (xhr.readyState == 4 && xhr.status == 200) { 104 | console.log(new Date()); 105 | // Every thing ok, file uploaded 106 | console.log(xhr.responseText); // handle response. 107 | var res = JSON.parse(xhr.responseText); 108 | if(res.code ==200){ 109 | cuteAPI.send(); 110 | } 111 | } 112 | }; 113 | 114 | fd.append("upload_file", f); 115 | fd.append("filename",file.name); 116 | fd.append('chunks',chunks); 117 | fd.append('currchunk',currchunk); 118 | fd.append('uid',this.uid); 119 | console.log("send:"); 120 | console.log(new Date()); 121 | xhr.send(fd); 122 | this.currBlock = block; 123 | this.xhr = xhr; 124 | } 125 | } 126 | 127 | 128 | 129 | -------------------------------------------------------------------------------- /java-upload/src/main/webapp/static/js/chunk.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | function CuteFile(file, chunkSize,onprogress,cutomsSuccess) { 4 | this.uid = (new Date()).getTime(); 5 | chunkSize = chunkSize || 2 * 1024 * 1024; 6 | var pending = [], 7 | blob = file, 8 | total = blob.size, 9 | chunks = chunkSize ? Math.ceil(total / chunkSize) : 1, 10 | start = 0, 11 | index = 0, 12 | len, api; 13 | 14 | while (index < chunks) { 15 | len = Math.min(chunkSize, total - start); 16 | 17 | pending.push({ 18 | file: file, 19 | start: start, 20 | end: chunkSize ? (start + len) : total, 21 | total: total, 22 | chunks: chunks, 23 | chunk: index++, 24 | cuted: api 25 | }); 26 | start += len; 27 | } 28 | 29 | file.blocks = pending.concat(); 30 | file.remaning = pending.length; 31 | this.file = file; 32 | this.pending = pending; 33 | this.onprogress = onprogress; 34 | this.cutomsSuccess = cutomsSuccess; 35 | } 36 | 37 | CuteFile.prototype = { 38 | has: function() { 39 | return !!this.pending.length; 40 | }, 41 | 42 | shift: function() { 43 | return this.pending.shift(); 44 | }, 45 | 46 | unshift: function(block) { 47 | this.pending.unshift(block); 48 | }, 49 | stop : function(){ 50 | if(!this.xhr) return; 51 | this.xhr.upload.onprogress = null; 52 | this.xhr.onreadystatechange = null; 53 | this.xhr.abort(); 54 | this.unshift(this.currBlock); 55 | this.isStart = false; 56 | }, 57 | start : function(){ 58 | this.isStart = true; 59 | this.send(); 60 | }, 61 | switchs : function(){ 62 | if(this.isStart){ 63 | this.stop(); 64 | }else{ 65 | this.start(); 66 | } 67 | }, 68 | success : function(){ 69 | this.cutomsSuccess && this.cutomsSuccess(this.file.uid); 70 | }, 71 | send : function (){ 72 | var cuteAPI = this; 73 | if(!cuteAPI.has() ){ 74 | this.isStart = false; 75 | this.success(); 76 | return false; 77 | } 78 | var block = cuteAPI.shift(); 79 | var file = block.file; 80 | var f = file.slice(block.start, block.end); 81 | 82 | var chunks = block.chunks; 83 | var currchunk = block.chunk; 84 | var xhr = new XMLHttpRequest(); 85 | xhr.open('POST','/partupload',true); 86 | var fd = new FormData(); 87 | xhr.upload.onprogress = function(data){ 88 | var result = {}; 89 | result.total = block.total; 90 | result.loaded = block.start + data.loaded; 91 | 92 | //防止进度条后退 93 | console.log(cuteAPI.currLoaded,result.loaded); 94 | if(cuteAPI.currLoaded > result.loaded){ 95 | result.loaded = this.currLoaded; 96 | }else{ 97 | cuteAPI.currLoaded = result.loaded; 98 | } 99 | //total 100 | cuteAPI.onprogress && cuteAPI.onprogress(file,result); 101 | } 102 | xhr.onreadystatechange = function() { 103 | if (xhr.readyState == 4 && xhr.status == 200) { 104 | console.log(new Date()); 105 | // Every thing ok, file uploaded 106 | console.log(xhr.responseText); // handle response. 107 | var res = JSON.parse(xhr.responseText); 108 | if(res.code ==200){ 109 | cuteAPI.send(); 110 | } 111 | } 112 | }; 113 | 114 | fd.append("upload_file", f); 115 | fd.append("filename",file.name); 116 | fd.append('chunks',chunks); 117 | fd.append('currchunk',currchunk); 118 | fd.append('uid',this.uid); 119 | console.log("send:"); 120 | console.log(new Date()); 121 | xhr.send(fd); 122 | this.currBlock = block; 123 | this.xhr = xhr; 124 | } 125 | } 126 | 127 | 128 | 129 | -------------------------------------------------------------------------------- /java-upload/src/main/webapp/static/demo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 |
13 | 14 |
15 | 16 |
17 |

18 | 19 |
20 | 21 | 22 |
23 | 24 |
25 |
26 | 27 |
28 | 29 | 30 |
31 |
32 | command + shift + J 33 |
34 | 35 | 36 | 37 | 157 | 158 | 172 | 173 | 174 | -------------------------------------------------------------------------------- /node-upload/static/uploadMethod/corsUpload.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | CORS-ajax上传 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 |
h5 ajax带进度条上传文件
15 |
16 |
17 | 选择文件 18 | 19 | 20 |
21 |
    22 |
23 | 24 | 25 |
26 |
27 | 28 | 29 | 112 | 113 | -------------------------------------------------------------------------------- /node-upload/static/uploadMethod/iframeUpload.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | iframe表单上传 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 |
iframe表单异步上传文件
15 |
16 | 26 | 27 | 28 |
29 |
30 | 31 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /java-upload/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 23 | 4.0.0 24 | war 25 | 26 | uploadserver 27 | uploadserver 28 | uploadserver 29 | 1.0 30 | 31 | 32 | ${project.basedir}/target/classes/ 33 | 34 | 35 | org.mortbay.jetty 36 | maven-jetty-plugin 37 | 6.1.7 38 | 39 | 0 40 | 41 | 42 | 8888 43 | 30000 44 | 45 | 46 | / 47 | 48 | 49 | 50 | org.apache.maven.plugins 51 | maven-war-plugin 52 | 2.3 53 | 54 | ${project.artifactId}-${project.version} 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | UTF-8 63 | 9.2.7.v20150116 64 | 65 | 66 | 67 | 72 | 73 | 74 | org.springframework 75 | spring-webmvc 76 | 3.2.8.RELEASE 77 | 78 | 79 | 80 | org.springframework 81 | spring-web 82 | 3.2.8.RELEASE 83 | 84 | 85 | 86 | javax.servlet 87 | servlet-api 88 | 2.5 89 | provided 90 | 91 | 92 | javax.servlet.jsp 93 | jsp-api 94 | 2.1 95 | provided 96 | 97 | 98 | 99 | com.fasterxml.jackson.core 100 | jackson-core 101 | 2.2.3 102 | 103 | 104 | com.fasterxml.jackson.core 105 | jackson-databind 106 | 2.2.3 107 | 108 | 109 | com.fasterxml.jackson.core 110 | jackson-annotations 111 | 2.2.3 112 | 113 | 114 | 115 | commons-fileupload 116 | commons-fileupload 117 | 1.3.1 118 | 119 | 120 | 121 | org.eclipse.jetty.websocket 122 | websocket-api 123 | ${jetty-version} 124 | 125 | 126 | 127 | org.eclipse.jetty.websocket 128 | websocket-server 129 | ${jetty-version} 130 | 131 | 132 | 133 | org.eclipse.jetty.websocket 134 | websocket-client 135 | ${jetty-version} 136 | 137 | 138 | com.google.protobuf 139 | protobuf-java 140 | 2.5.0 141 | 142 | 143 | 144 | 145 | 146 | 147 | -------------------------------------------------------------------------------- /java-upload/src/main/webapp/static/uploadMethod/iframeUpload.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 |
iframe表单异步上传文件
15 |
16 | 26 | 27 | 28 |
29 |
30 | 31 | 32 | 50 | 51 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /node-upload/server.js: -------------------------------------------------------------------------------- 1 | /** 2 | * node版上传Demo 3 | * 代码粗陋,只能用于学习 4 | * @Author SAI 5 | * @DateTime 2016-11-12T19:43:12+0800 6 | */ 7 | 8 | const path = require('path'); 9 | const koa = require('koa'); 10 | const serve = require('koa-static'); 11 | const formidable = require('formidable'); 12 | const util = require('util'); 13 | const body = require('koa-better-body'); 14 | const fs = require('fs'); 15 | const stringify = require('json-stable-stringify'); 16 | const contentType = require('content-type-mime'); 17 | const cors = require('koa-cors'); 18 | 19 | var router = require('koa-router')(); 20 | var app = koa(); 21 | 22 | const _static = serve(path.join(__dirname,'static')); 23 | _static._name = 'static static'; 24 | 25 | 26 | function _upload (req,res,success) { 27 | 28 | var form = new formidable.IncomingForm(); 29 | var path = req.headers.referer; 30 | form.uploadDir = __dirname+'/uploadFile'; 31 | form.keepExtensions = true; 32 | form.uploadPath = function(filename){ 33 | return filename; 34 | } 35 | var successFlag = false; 36 | form.parse(req, function (err, fields, files) { 37 | if(!err){ 38 | success && success(files.inputFile.name,'/uploadFile/'+files.inputFile.name); 39 | }else{ 40 | } 41 | }); 42 | } 43 | 44 | /** 45 | * 保存完整文件 46 | * @Author xiangsai.huang 47 | * @DateTime 2016-11-14T16:08:42+0800 48 | * @param {[string]} name [文件名] 49 | * @param {[string]} path [保存的路径] 50 | */ 51 | function saveFile(name,path){ 52 | var f1 = fs.readFileSync(path); 53 | fs.writeFileSync(__dirname+'/uploadFile/'+name, f1); 54 | fs.unlinkSync(path); 55 | var result = { 56 | data : [{ 57 | name : name, 58 | filePath : '/'+name, 59 | msg : 'success' 60 | }] 61 | } 62 | return result; 63 | } 64 | 65 | /** 66 | * 保存分片文件 67 | * @Author xiangsai.huang 68 | * @DateTime 2016-11-15T09:56:41+0800 69 | */ 70 | function saveChunk(params){ 71 | try{ 72 | var id = params.id,chunk = params.chunk,path = params.path,name = params.name; 73 | var f1 = fs.readFileSync(path); 74 | var _path = __dirname+'/uploadFile/'+id+'/'; 75 | if(!fs.existsSync(_path)){ 76 | fs.mkdirSync(_path); 77 | } 78 | fs.writeFileSync(__dirname+'/uploadFile/'+id+'/'+name+'_'+chunk, f1); 79 | fs.unlinkSync(path); 80 | var result = {code : 200} 81 | }catch(e){ 82 | return {code : -1} 83 | } 84 | return result; 85 | } 86 | 87 | /** 88 | * 普通表单上传文件 89 | * @Author xiangsai.huang 90 | * @DateTime 2016-11-12T19:43:12+0800 91 | */ 92 | router.post('/formUplaod',function *(){ 93 | var self = this; 94 | var req = this.req , res = this.res ; 95 | var path = this.req.headers.referer; 96 | _upload(req,res); 97 | self.redirect(path); 98 | }); 99 | 100 | /** 101 | * iframe上传文件 102 | * @Author xiangsai.huang 103 | * @DateTime 2016-11-12T19:43:12+0800 104 | */ 105 | router.post('/iframeUpload',body({files : 'inputFile'}),function *(next){ 106 | var f = this.request.inputFile[0]; 107 | var result = saveFile(f.name,f.path); 108 | this.body = stringify(result); 109 | }); 110 | 111 | /** 112 | * ajax上传文件 113 | * @Author xiangsai.huang 114 | * @DateTime 2016-11-12T19:43:12+0800 115 | */ 116 | router.post('/ajaxUpload',body({files : 'inputFile'}),function *(){ 117 | var f = this.request.inputFile[0]; 118 | var result = saveFile(f.name,f.path); 119 | console.log(result); 120 | this.body = stringify(result); 121 | }); 122 | 123 | /** 124 | * cors-ajax上传文件 125 | * @Author xiangsai.huang 126 | * @DateTime 2016-11-12T19:43:12+0800 127 | */ 128 | router.post('/corsUpload',body({files : 'inputFile'}),function *(){ 129 | var f = this.request.inputFile[0]; 130 | var result = saveFile(f.name,f.path); 131 | console.log(result); 132 | this.body = stringify(result); 133 | this.set('Access-Control-Allow-Origin','*'); 134 | }); 135 | 136 | /** 137 | * 分片上传文件 138 | * @Author xiangsai.huang 139 | * @DateTime 2016-11-12T19:43:12+0800 140 | */ 141 | router.post('/chunkUpload',body({files : 'inputFile'}),function *(){ 142 | var fields = this.request.fields; 143 | var f = this.request.inputFile[0]; 144 | var result = saveChunk({id : fields.uid ,chunk : fields.currchunk, path : f.path,name : fields.filename }); 145 | this.body = stringify(result); 146 | }) 147 | 148 | /** 149 | * 获得已上传的文件 150 | * @Author xiangsai.huang 151 | * @DateTime 2016-11-12T19:43:12+0800 152 | */ 153 | router.get('/filesList',function *(){ 154 | var _path = path.join(__dirname,'uploadFile'); 155 | var filenames = fs.readdirSync(_path); 156 | var resultPaths = []; 157 | for(var i = 0 ; i < filenames.length ; i++){ 158 | var name = filenames[i]; 159 | var lstat = fs.lstatSync(_path+'/'+name); 160 | if( !(/^\./.test(name)) && lstat.isDirectory()){ 161 | var names = fs.readdirSync(path.join(__dirname,'uploadFile',name)); 162 | var n = names[0].replace(/_\d*$/,''); 163 | resultPaths.push({path : '/file/'+name+'/'+n}); 164 | }else if(!(/^\./.test(name)) && lstat.isFile()){ 165 | resultPaths.push({path : '/'+name}); 166 | } 167 | 168 | } 169 | this.body = {data : resultPaths} 170 | }) 171 | 172 | /** 173 | * 获得单个文件 174 | * @Author xiangsai.huang 175 | * @DateTime 2016-11-15T09:56:06+0800 176 | */ 177 | router.get('/file/:id/:name',function *(){ 178 | var id = this.params.id; 179 | var name = this.params.name; 180 | var fileList = []; 181 | var _path = path.join(__dirname,'uploadFile',id); 182 | var filenames = fs.readdirSync(_path); 183 | for(var i = 0 ; i < filenames.length ; i++){ 184 | var file = fs.readFileSync(path.join(_path,name+"_"+i), ''); 185 | fileList.push(file); 186 | } 187 | console.log(fileList.length); 188 | var buffer = Buffer.concat(fileList); 189 | console.log(Buffer.byteLength(buffer)); 190 | this.set('Content-Length',Buffer.byteLength(buffer)); 191 | this.type = contentType(name); 192 | this.body = buffer; 193 | }) 194 | 195 | 196 | app 197 | .use(cors()) 198 | .use(_static) 199 | .use(serve(path.join(__dirname,'uploadFile'))) 200 | .use(router.routes()) 201 | .use(router.allowedMethods()) 202 | .listen(8083); 203 | -------------------------------------------------------------------------------- /node-upload/static/uploadMethod/chunkUpload.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ajax上传 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
分片上传
16 |
17 |
18 | 选择文件 19 | 20 | 21 | 22 |
23 |
    24 |
25 | 26 |
27 |
28 | 29 | 30 |
31 |
断点续传(可暂停)
32 |
33 |
34 | 选择文件 35 | 36 | 37 | 38 |
39 |
    40 |
41 | 42 | 43 |
44 |
45 | 46 | 47 | 148 | 149 | -------------------------------------------------------------------------------- /java-upload/src/main/webapp/static/uploadMethod/chunkUpload.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ajax上传 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
分片上传
16 |
17 |
18 | 选择文件 19 | 20 | 21 | 22 |
23 |
    24 |
25 | 26 |
27 |
28 | 29 | 30 |
31 |
断点续传(可暂停)
32 |
33 |
34 | 选择文件 35 | 36 | 37 | 38 |
39 |
    40 |
41 | 42 | 43 |
44 |
45 | 46 | 47 | 148 | 149 | -------------------------------------------------------------------------------- /node-upload/static/uploadMethod/ajaxUpload.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ajax上传 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 |
ajax上传文件
15 |
16 |
17 | 选择文件 18 | 19 | 20 |
21 |
    22 |
23 |
    24 |
25 | 26 |
27 |
28 | 29 |
30 |
h5 ajax带进度条上传文件
31 |
32 |
33 | 选择文件 34 | 35 | 36 |
37 |
    38 |
39 | 40 | 41 |
42 |
43 | 44 | 45 | 141 | 142 | -------------------------------------------------------------------------------- /java-upload/src/main/webapp/static/uploadMethod/wsUpload.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ajax上传 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 |
websocket上传文件
17 |
18 |
19 | 选择文件 20 | 21 | 22 |
23 |
    24 |
25 |
    26 |
27 |
28 |
29 |
30 | 31 | 32 | 152 | 153 | 154 | -------------------------------------------------------------------------------- /node-upload/static/uploadMethod/wsUpload.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ajax上传 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 |
websocket上传文件
17 |
18 |
19 | 选择文件 20 | 21 | 22 |
23 |
    24 |
25 |
    26 |
27 |
28 |
29 |
30 | 31 | 32 | 153 | 154 | 155 | -------------------------------------------------------------------------------- /java-upload/src/main/webapp/static/uploadMethod/ajaxUpload.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ajax上传 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
ajax上传文件
16 |
17 |
18 | 选择文件 19 | 20 | 21 | 22 |
23 |
    24 |
25 |
    26 | 27 |
28 | 29 |
30 |
31 | 32 | 33 |
34 |
h5 ajax带进度条上传文件
35 |
36 |
37 | 选择文件 38 | 39 | 40 | 41 |
42 |
    43 |
44 | 45 | 46 |
47 |
48 | 49 | 50 | 162 | 163 | -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/ctrl/UploadController.java: -------------------------------------------------------------------------------- 1 | package com.devsai.ctrl; 2 | import org.springframework.stereotype.Controller; 3 | import org.springframework.web.bind.annotation.*; 4 | import org.springframework.web.multipart.MultipartFile; 5 | import org.springframework.web.multipart.MultipartHttpServletRequest; 6 | import org.springframework.web.servlet.ModelAndView; 7 | import javax.servlet.http.Cookie; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | import java.io.*; 11 | import java.text.SimpleDateFormat; 12 | import java.util.*; 13 | 14 | /** 15 | * Created by huangxiangsai on 16/4/19. 16 | * web上传Demo , 17 | * 代码粗陋,只适用于学习 18 | */ 19 | @Controller 20 | 21 | public class UploadController { 22 | 23 | private static String tempFilePath = System.getProperty("user.dir")+"/src/main/webapp/static/tempFiles/"; 24 | 25 | @RequestMapping(value="formUpload",method = {RequestMethod.POST,RequestMethod.GET} ) 26 | public ModelAndView formUpload(HttpServletRequest request,HttpServletResponse response,@RequestParam("inputFile") MultipartFile file){ 27 | System.out.println(file.getOriginalFilename()); 28 | try { 29 | 30 | FileOutputStream out = new FileOutputStream(new File(tempFilePath+file.getOriginalFilename())); 31 | out.write(file.getBytes()); 32 | out.close(); 33 | } catch (IOException e) { 34 | e.printStackTrace(); 35 | } 36 | ModelAndView view = new ModelAndView("redirect:/static/uploadMethod/formUpload.html"); 37 | return view; 38 | } 39 | 40 | 41 | @RequestMapping(value="iframeUpload",method = {RequestMethod.POST,RequestMethod.GET} ) 42 | @ResponseBody 43 | public CommonResult iframeUpload(HttpServletRequest request,HttpServletResponse response,@RequestParam("inputFile") MultipartFile file){ 44 | System.out.println(file.getOriginalFilename()); 45 | try { 46 | // response.set 47 | FileOutputStream out = new FileOutputStream(new File(tempFilePath+file.getOriginalFilename())); 48 | out.write(file.getBytes()); 49 | out.close(); 50 | } catch (IOException e) { 51 | e.printStackTrace(); 52 | } 53 | 54 | 55 | Cookie c = new Cookie("h","d"); 56 | c.setDomain("local.com"); 57 | response.addCookie(c); 58 | CommonResult cr = new CommonResult(); 59 | cr.setCode(200); 60 | cr.setMessage("成功"); 61 | List> result = new ArrayList>(); 62 | Map map = new HashMap(); 63 | map.put("filePath", "/static/tempFiles/" + file.getOriginalFilename()); 64 | result.add(map); 65 | cr.setData(result); 66 | return cr; 67 | } 68 | 69 | /** 70 | * 普通ajax上传 71 | * @return 72 | */ 73 | @RequestMapping(value="upload",method = RequestMethod.POST) 74 | @ResponseBody 75 | public CommonResult normalUpload(MultipartHttpServletRequest multipartRequest,HttpServletResponse resonse){//MultipartRequest multipartRequest 76 | MultipartFile file = multipartRequest.getFile("upload_file"); 77 | CommonResult cr = new CommonResult(); 78 | try { 79 | FileOutputStream out = new FileOutputStream(new File(tempFilePath+file.getOriginalFilename())); 80 | out.write(file.getBytes()); 81 | out.close(); 82 | cr.setCode(200); 83 | cr.setMessage("成功"); 84 | List> result = new ArrayList>(); 85 | Map map = new HashMap(); 86 | map.put("filePath", "/static/tempFiles/" + file.getOriginalFilename()); 87 | map.put("name",file.getOriginalFilename()); 88 | result.add(map); 89 | cr.setData(result); 90 | } catch (IOException e) { 91 | cr.setCode(500); 92 | cr.setMessage("上传失败"); 93 | e.printStackTrace(); 94 | } 95 | return cr; 96 | } 97 | 98 | 99 | 100 | /** 101 | * 分片上传 102 | * @param resonse 103 | * @param request 104 | * @param multipartRequest 105 | * @return 106 | */ 107 | @RequestMapping(value="partupload",method = RequestMethod.POST) 108 | @ResponseBody 109 | public CommonResult partUpload(HttpServletResponse resonse,HttpServletRequest request,MultipartHttpServletRequest multipartRequest){ 110 | CommonResult cr = new CommonResult(); 111 | cr.setCode(200); 112 | cr.setMessage("成功"); 113 | 114 | MultipartFile file = multipartRequest.getFile("upload_file"); 115 | Integer chunks = (Integer.valueOf(request.getParameter("chunks"))); 116 | Long uid = (Long.valueOf(request.getParameter("uid"))); 117 | Integer currchunk = (Integer.valueOf(request.getParameter("currchunk"))); 118 | String filename = request.getParameter("filename"); 119 | 120 | if(!this.saveFile(file,filename,chunks,currchunk,uid)){ 121 | cr.setCode(500); 122 | cr.setMessage("上传失败"); 123 | } 124 | 125 | return cr; 126 | } 127 | 128 | /** 129 | * 服务端分片 130 | * @param resonse 131 | * @param request 132 | * @param multipartRequest 133 | * @return 134 | */ 135 | @RequestMapping(value="rangeupload",method = RequestMethod.POST) 136 | @ResponseBody 137 | public CommonResult serverPartUpload(HttpServletResponse resonse,HttpServletRequest request,MultipartHttpServletRequest multipartRequest){ 138 | CommonResult cr = new CommonResult(); 139 | cr.setCode(200); 140 | cr.setMessage("成功"); 141 | 142 | return cr; 143 | } 144 | 145 | 146 | /** 147 | * 保存上传的文件 148 | * @param file //上传的文件 149 | * @param chunks //分片总数 (不分片上传时,后两参数为0) 150 | * @param currChunk //当前的分片 (不分片上传时,后两参数为0) 151 | */ 152 | public boolean saveFile(MultipartFile file, String fileName, int chunks , int currChunk,Long uid){ 153 | try { 154 | Date date = new Date(); 155 | Calendar cd = Calendar.getInstance(); 156 | SimpleDateFormat sf = new SimpleDateFormat("yy-MM-dd"); 157 | Long time = System.currentTimeMillis(); 158 | String path = tempFilePath+"/"+sf.format(time)+"/"+uid; 159 | this.createDir(path); 160 | FileOutputStream out = new FileOutputStream(new File(path+"/"+fileName+".part_"+currChunk)); 161 | out.write(file.getBytes()); 162 | out.close(); 163 | return true; 164 | } catch (IOException e) { 165 | e.printStackTrace(); 166 | } 167 | return false; 168 | } 169 | 170 | 171 | private void createDir(String path){ 172 | File dir = new File(path); 173 | if(!dir.exists()){ 174 | dir.mkdirs(); 175 | } 176 | } 177 | 178 | 179 | @RequestMapping(value="/getFile/{dir}/{uid}/{fileName}", method = RequestMethod.GET) 180 | public void getFile(@PathVariable("dir") String dir ,@PathVariable("uid") String uid , @PathVariable("fileName") String fileName , 181 | HttpServletResponse resonse){ 182 | String path = tempFilePath+"/"+dir+"/"+uid; 183 | 184 | File file = new File(path); 185 | File[] filelist= file.listFiles(); 186 | List result = new ArrayList(); 187 | File[] list = new File[filelist.length]; 188 | //过滤出文件片段 189 | for(int i = 0 ; i < filelist.length ; i++){ 190 | File f = filelist[i]; 191 | String name = f.getName(); 192 | if(name.indexOf(fileName) != -1){ 193 | list[i] = f; 194 | } 195 | } 196 | 197 | //按时间排序 198 | for(int i = 0 ; i < list.length ; i++){ 199 | for(int j = i+1 ; j < result.size() ; j++){ 200 | if(list[i].lastModified() > list[j].lastModified()){ 201 | File temp = list[j]; 202 | list[i] = list[j]; 203 | list[j] = temp; 204 | } 205 | } 206 | } 207 | 208 | try { 209 | OutputStream os = resonse.getOutputStream(); 210 | List bytelist = new ArrayList(); 211 | int len = 0; 212 | for(int i = 0 ; i < list.length ; i++){ 213 | byte[] partFile= this.getPartFileByFile(list[i]); 214 | bytelist.add(partFile); 215 | os.write(partFile); 216 | } 217 | System.out.println(list.length); 218 | os.flush(); 219 | os.close(); 220 | } catch (IOException e) { 221 | e.printStackTrace(); 222 | } 223 | } 224 | 225 | 226 | /** 227 | * 获得已上传的文件列表 228 | * @param resonse 229 | * @param request 230 | * @param 231 | * @return 232 | */ 233 | @RequestMapping(value="fileList",method = RequestMethod.GET) 234 | @ResponseBody 235 | public CommonResult fileList(HttpServletResponse resonse,HttpServletRequest request){ 236 | CommonResult cr = new CommonResult(); 237 | cr.setCode(200); 238 | Map data = new HashMap(); 239 | List> list = new ArrayList>(); 240 | File dir = new File(tempFilePath); 241 | String[] dateDir = dir.list(); 242 | for(int i = 0 ; i < dateDir.length ; i++){ 243 | File f = new File(tempFilePath+"/"+dateDir[i]); 244 | if(f.isDirectory()){ 245 | System.out.println(tempFilePath+"/"+dateDir[i]); 246 | File[] fs = f.listFiles((FileFilter)MyHiddenFileFilter.HIDDEN); 247 | System.out.println(fs.length); 248 | String[] timeDir = f.list(); 249 | for(int j = 0 ; j < timeDir.length; j++){ 250 | File f2 = new File(tempFilePath+"/"+dateDir[i]+"/"+timeDir[j]); 251 | if(f2.list().length > 0){ 252 | String name = f2.list()[0]; 253 | name = name.replace(".part_0",""); 254 | Map d = new HashMap(); 255 | d.put("fileName",name); 256 | d.put("filePath","/getFile/"+dateDir[i]+"/"+timeDir[j]+"/"+name); 257 | list.add(d); 258 | } 259 | } 260 | } 261 | } 262 | 263 | cr.setData(list); 264 | return cr; 265 | } 266 | 267 | private byte[] getPartFileByFile(File file){ 268 | try { 269 | InputStream input = new FileInputStream(file); 270 | byte[] result = new byte[(int)file.length()]; 271 | input.read(result); 272 | input.close(); 273 | return result; 274 | } catch (FileNotFoundException e) { 275 | e.printStackTrace(); 276 | } catch (IOException e) { 277 | e.printStackTrace(); 278 | } 279 | return null; 280 | } 281 | 282 | public void componentFile(String fileName , int chunks){ 283 | 284 | List list = new ArrayList(); 285 | for(int i = 0 ; i < chunks ;i++){ 286 | byte[] partFile= this.getPartFile(fileName,i); 287 | System.out.println(partFile.length); 288 | list.add(partFile); 289 | } 290 | 291 | FileOutputStream out = null; 292 | try { 293 | out = new FileOutputStream(new File("/Users/huangxiangsai/Desktop/u_"+fileName)); 294 | for(int i = 0 ; i < list.size(); i++){ 295 | out.write(list.get(i)); 296 | } 297 | out.close(); 298 | 299 | } catch (FileNotFoundException e) { 300 | e.printStackTrace(); 301 | } catch (IOException e) { 302 | e.printStackTrace(); 303 | } 304 | } 305 | 306 | 307 | private byte[] getPartFile(String fileName,int currChunk){ 308 | try { 309 | File file = new File("/Users/huangxiangsai/Desktop/u_"+fileName+".part_"+currChunk); 310 | InputStream input = new FileInputStream(file); 311 | byte[] result = new byte[(int)file.length()]; 312 | input.read(result); 313 | input.close(); 314 | return result; 315 | } catch (FileNotFoundException e) { 316 | e.printStackTrace(); 317 | } catch (IOException e) { 318 | e.printStackTrace(); 319 | } 320 | return null; 321 | } 322 | } -------------------------------------------------------------------------------- /java-upload/src/main/java/com/devsai/model/UploadMsg.java: -------------------------------------------------------------------------------- 1 | package com.devsai.model;// Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: webapp/static/proto/upload.proto 3 | 4 | public final class UploadMsg { 5 | private UploadMsg() {} 6 | public static void registerAllExtensions( 7 | com.google.protobuf.ExtensionRegistry registry) { 8 | } 9 | public interface UploadOrBuilder extends 10 | // @@protoc_insertion_point(interface_extends:Upload) 11 | com.google.protobuf.MessageOrBuilder { 12 | 13 | /** 14 | * required string filename = 1; 15 | */ 16 | boolean hasFilename(); 17 | /** 18 | * required string filename = 1; 19 | */ 20 | java.lang.String getFilename(); 21 | /** 22 | * required string filename = 1; 23 | */ 24 | com.google.protobuf.ByteString 25 | getFilenameBytes(); 26 | 27 | /** 28 | * required int32 currchunk = 2; 29 | */ 30 | boolean hasCurrchunk(); 31 | /** 32 | * required int32 currchunk = 2; 33 | */ 34 | int getCurrchunk(); 35 | 36 | /** 37 | * required int32 chunks = 3; 38 | */ 39 | boolean hasChunks(); 40 | /** 41 | * required int32 chunks = 3; 42 | */ 43 | int getChunks(); 44 | 45 | /** 46 | * required int32 uid = 4; 47 | */ 48 | boolean hasUid(); 49 | /** 50 | * required int32 uid = 4; 51 | */ 52 | int getUid(); 53 | 54 | /** 55 | * required bytes upload_file = 5; 56 | */ 57 | boolean hasUploadFile(); 58 | /** 59 | * required bytes upload_file = 5; 60 | */ 61 | com.google.protobuf.ByteString getUploadFile(); 62 | } 63 | /** 64 | * Protobuf type {@code Upload} 65 | */ 66 | public static final class Upload extends 67 | com.google.protobuf.GeneratedMessage implements 68 | // @@protoc_insertion_point(message_implements:Upload) 69 | UploadOrBuilder { 70 | // Use Upload.newBuilder() to construct. 71 | private Upload(com.google.protobuf.GeneratedMessage.Builder builder) { 72 | super(builder); 73 | this.unknownFields = builder.getUnknownFields(); 74 | } 75 | private Upload(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); } 76 | 77 | private static final Upload defaultInstance; 78 | public static Upload getDefaultInstance() { 79 | return defaultInstance; 80 | } 81 | 82 | public Upload getDefaultInstanceForType() { 83 | return defaultInstance; 84 | } 85 | 86 | private final com.google.protobuf.UnknownFieldSet unknownFields; 87 | @java.lang.Override 88 | public final com.google.protobuf.UnknownFieldSet 89 | getUnknownFields() { 90 | return this.unknownFields; 91 | } 92 | private Upload( 93 | com.google.protobuf.CodedInputStream input, 94 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 95 | throws com.google.protobuf.InvalidProtocolBufferException { 96 | initFields(); 97 | int mutable_bitField0_ = 0; 98 | com.google.protobuf.UnknownFieldSet.Builder unknownFields = 99 | com.google.protobuf.UnknownFieldSet.newBuilder(); 100 | try { 101 | boolean done = false; 102 | while (!done) { 103 | int tag = input.readTag(); 104 | switch (tag) { 105 | case 0: 106 | done = true; 107 | break; 108 | default: { 109 | if (!parseUnknownField(input, unknownFields, 110 | extensionRegistry, tag)) { 111 | done = true; 112 | } 113 | break; 114 | } 115 | case 10: { 116 | com.google.protobuf.ByteString bs = input.readBytes(); 117 | bitField0_ |= 0x00000001; 118 | filename_ = bs; 119 | break; 120 | } 121 | case 16: { 122 | bitField0_ |= 0x00000002; 123 | currchunk_ = input.readInt32(); 124 | break; 125 | } 126 | case 24: { 127 | bitField0_ |= 0x00000004; 128 | chunks_ = input.readInt32(); 129 | break; 130 | } 131 | case 32: { 132 | bitField0_ |= 0x00000008; 133 | uid_ = input.readInt32(); 134 | break; 135 | } 136 | case 42: { 137 | bitField0_ |= 0x00000010; 138 | uploadFile_ = input.readBytes(); 139 | break; 140 | } 141 | } 142 | } 143 | } catch (com.google.protobuf.InvalidProtocolBufferException e) { 144 | throw e.setUnfinishedMessage(this); 145 | } catch (java.io.IOException e) { 146 | throw new com.google.protobuf.InvalidProtocolBufferException( 147 | e.getMessage()).setUnfinishedMessage(this); 148 | } finally { 149 | this.unknownFields = unknownFields.build(); 150 | makeExtensionsImmutable(); 151 | } 152 | } 153 | public static final com.google.protobuf.Descriptors.Descriptor 154 | getDescriptor() { 155 | return UploadMsg.internal_static_Upload_descriptor; 156 | } 157 | 158 | protected com.google.protobuf.GeneratedMessage.FieldAccessorTable 159 | internalGetFieldAccessorTable() { 160 | return UploadMsg.internal_static_Upload_fieldAccessorTable 161 | .ensureFieldAccessorsInitialized( 162 | UploadMsg.Upload.class, UploadMsg.Upload.Builder.class); 163 | } 164 | 165 | public static com.google.protobuf.Parser PARSER = 166 | new com.google.protobuf.AbstractParser() { 167 | public Upload parsePartialFrom( 168 | com.google.protobuf.CodedInputStream input, 169 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 170 | throws com.google.protobuf.InvalidProtocolBufferException { 171 | return new Upload(input, extensionRegistry); 172 | } 173 | }; 174 | 175 | @java.lang.Override 176 | public com.google.protobuf.Parser getParserForType() { 177 | return PARSER; 178 | } 179 | 180 | private int bitField0_; 181 | public static final int FILENAME_FIELD_NUMBER = 1; 182 | private java.lang.Object filename_; 183 | /** 184 | * required string filename = 1; 185 | */ 186 | public boolean hasFilename() { 187 | return ((bitField0_ & 0x00000001) == 0x00000001); 188 | } 189 | /** 190 | * required string filename = 1; 191 | */ 192 | public java.lang.String getFilename() { 193 | java.lang.Object ref = filename_; 194 | if (ref instanceof java.lang.String) { 195 | return (java.lang.String) ref; 196 | } else { 197 | com.google.protobuf.ByteString bs = 198 | (com.google.protobuf.ByteString) ref; 199 | java.lang.String s = bs.toStringUtf8(); 200 | if (bs.isValidUtf8()) { 201 | filename_ = s; 202 | } 203 | return s; 204 | } 205 | } 206 | /** 207 | * required string filename = 1; 208 | */ 209 | public com.google.protobuf.ByteString 210 | getFilenameBytes() { 211 | java.lang.Object ref = filename_; 212 | if (ref instanceof java.lang.String) { 213 | com.google.protobuf.ByteString b = 214 | com.google.protobuf.ByteString.copyFromUtf8( 215 | (java.lang.String) ref); 216 | filename_ = b; 217 | return b; 218 | } else { 219 | return (com.google.protobuf.ByteString) ref; 220 | } 221 | } 222 | 223 | public static final int CURRCHUNK_FIELD_NUMBER = 2; 224 | private int currchunk_; 225 | /** 226 | * required int32 currchunk = 2; 227 | */ 228 | public boolean hasCurrchunk() { 229 | return ((bitField0_ & 0x00000002) == 0x00000002); 230 | } 231 | /** 232 | * required int32 currchunk = 2; 233 | */ 234 | public int getCurrchunk() { 235 | return currchunk_; 236 | } 237 | 238 | public static final int CHUNKS_FIELD_NUMBER = 3; 239 | private int chunks_; 240 | /** 241 | * required int32 chunks = 3; 242 | */ 243 | public boolean hasChunks() { 244 | return ((bitField0_ & 0x00000004) == 0x00000004); 245 | } 246 | /** 247 | * required int32 chunks = 3; 248 | */ 249 | public int getChunks() { 250 | return chunks_; 251 | } 252 | 253 | public static final int UID_FIELD_NUMBER = 4; 254 | private int uid_; 255 | /** 256 | * required int32 uid = 4; 257 | */ 258 | public boolean hasUid() { 259 | return ((bitField0_ & 0x00000008) == 0x00000008); 260 | } 261 | /** 262 | * required int32 uid = 4; 263 | */ 264 | public int getUid() { 265 | return uid_; 266 | } 267 | 268 | public static final int UPLOAD_FILE_FIELD_NUMBER = 5; 269 | private com.google.protobuf.ByteString uploadFile_; 270 | /** 271 | * required bytes upload_file = 5; 272 | */ 273 | public boolean hasUploadFile() { 274 | return ((bitField0_ & 0x00000010) == 0x00000010); 275 | } 276 | /** 277 | * required bytes upload_file = 5; 278 | */ 279 | public com.google.protobuf.ByteString getUploadFile() { 280 | return uploadFile_; 281 | } 282 | 283 | private void initFields() { 284 | filename_ = ""; 285 | currchunk_ = 0; 286 | chunks_ = 0; 287 | uid_ = 0; 288 | uploadFile_ = com.google.protobuf.ByteString.EMPTY; 289 | } 290 | private byte memoizedIsInitialized = -1; 291 | public final boolean isInitialized() { 292 | byte isInitialized = memoizedIsInitialized; 293 | if (isInitialized == 1) return true; 294 | if (isInitialized == 0) return false; 295 | 296 | if (!hasFilename()) { 297 | memoizedIsInitialized = 0; 298 | return false; 299 | } 300 | if (!hasCurrchunk()) { 301 | memoizedIsInitialized = 0; 302 | return false; 303 | } 304 | if (!hasChunks()) { 305 | memoizedIsInitialized = 0; 306 | return false; 307 | } 308 | if (!hasUid()) { 309 | memoizedIsInitialized = 0; 310 | return false; 311 | } 312 | if (!hasUploadFile()) { 313 | memoizedIsInitialized = 0; 314 | return false; 315 | } 316 | memoizedIsInitialized = 1; 317 | return true; 318 | } 319 | 320 | public void writeTo(com.google.protobuf.CodedOutputStream output) 321 | throws java.io.IOException { 322 | getSerializedSize(); 323 | if (((bitField0_ & 0x00000001) == 0x00000001)) { 324 | output.writeBytes(1, getFilenameBytes()); 325 | } 326 | if (((bitField0_ & 0x00000002) == 0x00000002)) { 327 | output.writeInt32(2, currchunk_); 328 | } 329 | if (((bitField0_ & 0x00000004) == 0x00000004)) { 330 | output.writeInt32(3, chunks_); 331 | } 332 | if (((bitField0_ & 0x00000008) == 0x00000008)) { 333 | output.writeInt32(4, uid_); 334 | } 335 | if (((bitField0_ & 0x00000010) == 0x00000010)) { 336 | output.writeBytes(5, uploadFile_); 337 | } 338 | getUnknownFields().writeTo(output); 339 | } 340 | 341 | private int memoizedSerializedSize = -1; 342 | public int getSerializedSize() { 343 | int size = memoizedSerializedSize; 344 | if (size != -1) return size; 345 | 346 | size = 0; 347 | if (((bitField0_ & 0x00000001) == 0x00000001)) { 348 | size += com.google.protobuf.CodedOutputStream 349 | .computeBytesSize(1, getFilenameBytes()); 350 | } 351 | if (((bitField0_ & 0x00000002) == 0x00000002)) { 352 | size += com.google.protobuf.CodedOutputStream 353 | .computeInt32Size(2, currchunk_); 354 | } 355 | if (((bitField0_ & 0x00000004) == 0x00000004)) { 356 | size += com.google.protobuf.CodedOutputStream 357 | .computeInt32Size(3, chunks_); 358 | } 359 | if (((bitField0_ & 0x00000008) == 0x00000008)) { 360 | size += com.google.protobuf.CodedOutputStream 361 | .computeInt32Size(4, uid_); 362 | } 363 | if (((bitField0_ & 0x00000010) == 0x00000010)) { 364 | size += com.google.protobuf.CodedOutputStream 365 | .computeBytesSize(5, uploadFile_); 366 | } 367 | size += getUnknownFields().getSerializedSize(); 368 | memoizedSerializedSize = size; 369 | return size; 370 | } 371 | 372 | private static final long serialVersionUID = 0L; 373 | @java.lang.Override 374 | protected java.lang.Object writeReplace() 375 | throws java.io.ObjectStreamException { 376 | return super.writeReplace(); 377 | } 378 | 379 | public static UploadMsg.Upload parseFrom( 380 | com.google.protobuf.ByteString data) 381 | throws com.google.protobuf.InvalidProtocolBufferException { 382 | return PARSER.parseFrom(data); 383 | } 384 | public static UploadMsg.Upload parseFrom( 385 | com.google.protobuf.ByteString data, 386 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 387 | throws com.google.protobuf.InvalidProtocolBufferException { 388 | return PARSER.parseFrom(data, extensionRegistry); 389 | } 390 | public static UploadMsg.Upload parseFrom(byte[] data) 391 | throws com.google.protobuf.InvalidProtocolBufferException { 392 | return PARSER.parseFrom(data); 393 | } 394 | public static UploadMsg.Upload parseFrom( 395 | byte[] data, 396 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 397 | throws com.google.protobuf.InvalidProtocolBufferException { 398 | return PARSER.parseFrom(data, extensionRegistry); 399 | } 400 | public static UploadMsg.Upload parseFrom(java.io.InputStream input) 401 | throws java.io.IOException { 402 | return PARSER.parseFrom(input); 403 | } 404 | public static UploadMsg.Upload parseFrom( 405 | java.io.InputStream input, 406 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 407 | throws java.io.IOException { 408 | return PARSER.parseFrom(input, extensionRegistry); 409 | } 410 | public static UploadMsg.Upload parseDelimitedFrom(java.io.InputStream input) 411 | throws java.io.IOException { 412 | return PARSER.parseDelimitedFrom(input); 413 | } 414 | public static UploadMsg.Upload parseDelimitedFrom( 415 | java.io.InputStream input, 416 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 417 | throws java.io.IOException { 418 | return PARSER.parseDelimitedFrom(input, extensionRegistry); 419 | } 420 | public static UploadMsg.Upload parseFrom( 421 | com.google.protobuf.CodedInputStream input) 422 | throws java.io.IOException { 423 | return PARSER.parseFrom(input); 424 | } 425 | public static UploadMsg.Upload parseFrom( 426 | com.google.protobuf.CodedInputStream input, 427 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 428 | throws java.io.IOException { 429 | return PARSER.parseFrom(input, extensionRegistry); 430 | } 431 | 432 | public static Builder newBuilder() { return Builder.create(); } 433 | public Builder newBuilderForType() { return newBuilder(); } 434 | public static Builder newBuilder(UploadMsg.Upload prototype) { 435 | return newBuilder().mergeFrom(prototype); 436 | } 437 | public Builder toBuilder() { return newBuilder(this); } 438 | 439 | @java.lang.Override 440 | protected Builder newBuilderForType( 441 | com.google.protobuf.GeneratedMessage.BuilderParent parent) { 442 | Builder builder = new Builder(parent); 443 | return builder; 444 | } 445 | /** 446 | * Protobuf type {@code Upload} 447 | */ 448 | public static final class Builder extends 449 | com.google.protobuf.GeneratedMessage.Builder implements 450 | // @@protoc_insertion_point(builder_implements:Upload) 451 | UploadMsg.UploadOrBuilder { 452 | public static final com.google.protobuf.Descriptors.Descriptor 453 | getDescriptor() { 454 | return UploadMsg.internal_static_Upload_descriptor; 455 | } 456 | 457 | protected com.google.protobuf.GeneratedMessage.FieldAccessorTable 458 | internalGetFieldAccessorTable() { 459 | return UploadMsg.internal_static_Upload_fieldAccessorTable 460 | .ensureFieldAccessorsInitialized( 461 | UploadMsg.Upload.class, UploadMsg.Upload.Builder.class); 462 | } 463 | 464 | // Construct using UploadMsg.Upload.newBuilder() 465 | private Builder() { 466 | maybeForceBuilderInitialization(); 467 | } 468 | 469 | private Builder( 470 | com.google.protobuf.GeneratedMessage.BuilderParent parent) { 471 | super(parent); 472 | maybeForceBuilderInitialization(); 473 | } 474 | private void maybeForceBuilderInitialization() { 475 | if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) { 476 | } 477 | } 478 | private static Builder create() { 479 | return new Builder(); 480 | } 481 | 482 | public Builder clear() { 483 | super.clear(); 484 | filename_ = ""; 485 | bitField0_ = (bitField0_ & ~0x00000001); 486 | currchunk_ = 0; 487 | bitField0_ = (bitField0_ & ~0x00000002); 488 | chunks_ = 0; 489 | bitField0_ = (bitField0_ & ~0x00000004); 490 | uid_ = 0; 491 | bitField0_ = (bitField0_ & ~0x00000008); 492 | uploadFile_ = com.google.protobuf.ByteString.EMPTY; 493 | bitField0_ = (bitField0_ & ~0x00000010); 494 | return this; 495 | } 496 | 497 | public Builder clone() { 498 | return create().mergeFrom(buildPartial()); 499 | } 500 | 501 | public com.google.protobuf.Descriptors.Descriptor 502 | getDescriptorForType() { 503 | return UploadMsg.internal_static_Upload_descriptor; 504 | } 505 | 506 | public UploadMsg.Upload getDefaultInstanceForType() { 507 | return UploadMsg.Upload.getDefaultInstance(); 508 | } 509 | 510 | public UploadMsg.Upload build() { 511 | UploadMsg.Upload result = buildPartial(); 512 | if (!result.isInitialized()) { 513 | throw newUninitializedMessageException(result); 514 | } 515 | return result; 516 | } 517 | 518 | public UploadMsg.Upload buildPartial() { 519 | UploadMsg.Upload result = new UploadMsg.Upload(this); 520 | int from_bitField0_ = bitField0_; 521 | int to_bitField0_ = 0; 522 | if (((from_bitField0_ & 0x00000001) == 0x00000001)) { 523 | to_bitField0_ |= 0x00000001; 524 | } 525 | result.filename_ = filename_; 526 | if (((from_bitField0_ & 0x00000002) == 0x00000002)) { 527 | to_bitField0_ |= 0x00000002; 528 | } 529 | result.currchunk_ = currchunk_; 530 | if (((from_bitField0_ & 0x00000004) == 0x00000004)) { 531 | to_bitField0_ |= 0x00000004; 532 | } 533 | result.chunks_ = chunks_; 534 | if (((from_bitField0_ & 0x00000008) == 0x00000008)) { 535 | to_bitField0_ |= 0x00000008; 536 | } 537 | result.uid_ = uid_; 538 | if (((from_bitField0_ & 0x00000010) == 0x00000010)) { 539 | to_bitField0_ |= 0x00000010; 540 | } 541 | result.uploadFile_ = uploadFile_; 542 | result.bitField0_ = to_bitField0_; 543 | onBuilt(); 544 | return result; 545 | } 546 | 547 | public Builder mergeFrom(com.google.protobuf.Message other) { 548 | if (other instanceof UploadMsg.Upload) { 549 | return mergeFrom((UploadMsg.Upload)other); 550 | } else { 551 | super.mergeFrom(other); 552 | return this; 553 | } 554 | } 555 | 556 | public Builder mergeFrom(UploadMsg.Upload other) { 557 | if (other == UploadMsg.Upload.getDefaultInstance()) return this; 558 | if (other.hasFilename()) { 559 | bitField0_ |= 0x00000001; 560 | filename_ = other.filename_; 561 | onChanged(); 562 | } 563 | if (other.hasCurrchunk()) { 564 | setCurrchunk(other.getCurrchunk()); 565 | } 566 | if (other.hasChunks()) { 567 | setChunks(other.getChunks()); 568 | } 569 | if (other.hasUid()) { 570 | setUid(other.getUid()); 571 | } 572 | if (other.hasUploadFile()) { 573 | setUploadFile(other.getUploadFile()); 574 | } 575 | this.mergeUnknownFields(other.getUnknownFields()); 576 | return this; 577 | } 578 | 579 | public final boolean isInitialized() { 580 | if (!hasFilename()) { 581 | 582 | return false; 583 | } 584 | if (!hasCurrchunk()) { 585 | 586 | return false; 587 | } 588 | if (!hasChunks()) { 589 | 590 | return false; 591 | } 592 | if (!hasUid()) { 593 | 594 | return false; 595 | } 596 | if (!hasUploadFile()) { 597 | 598 | return false; 599 | } 600 | return true; 601 | } 602 | 603 | public Builder mergeFrom( 604 | com.google.protobuf.CodedInputStream input, 605 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 606 | throws java.io.IOException { 607 | UploadMsg.Upload parsedMessage = null; 608 | try { 609 | parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); 610 | } catch (com.google.protobuf.InvalidProtocolBufferException e) { 611 | parsedMessage = (UploadMsg.Upload) e.getUnfinishedMessage(); 612 | throw e; 613 | } finally { 614 | if (parsedMessage != null) { 615 | mergeFrom(parsedMessage); 616 | } 617 | } 618 | return this; 619 | } 620 | private int bitField0_; 621 | 622 | private java.lang.Object filename_ = ""; 623 | /** 624 | * required string filename = 1; 625 | */ 626 | public boolean hasFilename() { 627 | return ((bitField0_ & 0x00000001) == 0x00000001); 628 | } 629 | /** 630 | * required string filename = 1; 631 | */ 632 | public java.lang.String getFilename() { 633 | java.lang.Object ref = filename_; 634 | if (!(ref instanceof java.lang.String)) { 635 | com.google.protobuf.ByteString bs = 636 | (com.google.protobuf.ByteString) ref; 637 | java.lang.String s = bs.toStringUtf8(); 638 | if (bs.isValidUtf8()) { 639 | filename_ = s; 640 | } 641 | return s; 642 | } else { 643 | return (java.lang.String) ref; 644 | } 645 | } 646 | /** 647 | * required string filename = 1; 648 | */ 649 | public com.google.protobuf.ByteString 650 | getFilenameBytes() { 651 | java.lang.Object ref = filename_; 652 | if (ref instanceof String) { 653 | com.google.protobuf.ByteString b = 654 | com.google.protobuf.ByteString.copyFromUtf8( 655 | (java.lang.String) ref); 656 | filename_ = b; 657 | return b; 658 | } else { 659 | return (com.google.protobuf.ByteString) ref; 660 | } 661 | } 662 | /** 663 | * required string filename = 1; 664 | */ 665 | public Builder setFilename( 666 | java.lang.String value) { 667 | if (value == null) { 668 | throw new NullPointerException(); 669 | } 670 | bitField0_ |= 0x00000001; 671 | filename_ = value; 672 | onChanged(); 673 | return this; 674 | } 675 | /** 676 | * required string filename = 1; 677 | */ 678 | public Builder clearFilename() { 679 | bitField0_ = (bitField0_ & ~0x00000001); 680 | filename_ = getDefaultInstance().getFilename(); 681 | onChanged(); 682 | return this; 683 | } 684 | /** 685 | * required string filename = 1; 686 | */ 687 | public Builder setFilenameBytes( 688 | com.google.protobuf.ByteString value) { 689 | if (value == null) { 690 | throw new NullPointerException(); 691 | } 692 | bitField0_ |= 0x00000001; 693 | filename_ = value; 694 | onChanged(); 695 | return this; 696 | } 697 | 698 | private int currchunk_ ; 699 | /** 700 | * required int32 currchunk = 2; 701 | */ 702 | public boolean hasCurrchunk() { 703 | return ((bitField0_ & 0x00000002) == 0x00000002); 704 | } 705 | /** 706 | * required int32 currchunk = 2; 707 | */ 708 | public int getCurrchunk() { 709 | return currchunk_; 710 | } 711 | /** 712 | * required int32 currchunk = 2; 713 | */ 714 | public Builder setCurrchunk(int value) { 715 | bitField0_ |= 0x00000002; 716 | currchunk_ = value; 717 | onChanged(); 718 | return this; 719 | } 720 | /** 721 | * required int32 currchunk = 2; 722 | */ 723 | public Builder clearCurrchunk() { 724 | bitField0_ = (bitField0_ & ~0x00000002); 725 | currchunk_ = 0; 726 | onChanged(); 727 | return this; 728 | } 729 | 730 | private int chunks_ ; 731 | /** 732 | * required int32 chunks = 3; 733 | */ 734 | public boolean hasChunks() { 735 | return ((bitField0_ & 0x00000004) == 0x00000004); 736 | } 737 | /** 738 | * required int32 chunks = 3; 739 | */ 740 | public int getChunks() { 741 | return chunks_; 742 | } 743 | /** 744 | * required int32 chunks = 3; 745 | */ 746 | public Builder setChunks(int value) { 747 | bitField0_ |= 0x00000004; 748 | chunks_ = value; 749 | onChanged(); 750 | return this; 751 | } 752 | /** 753 | * required int32 chunks = 3; 754 | */ 755 | public Builder clearChunks() { 756 | bitField0_ = (bitField0_ & ~0x00000004); 757 | chunks_ = 0; 758 | onChanged(); 759 | return this; 760 | } 761 | 762 | private int uid_ ; 763 | /** 764 | * required int32 uid = 4; 765 | */ 766 | public boolean hasUid() { 767 | return ((bitField0_ & 0x00000008) == 0x00000008); 768 | } 769 | /** 770 | * required int32 uid = 4; 771 | */ 772 | public int getUid() { 773 | return uid_; 774 | } 775 | /** 776 | * required int32 uid = 4; 777 | */ 778 | public Builder setUid(int value) { 779 | bitField0_ |= 0x00000008; 780 | uid_ = value; 781 | onChanged(); 782 | return this; 783 | } 784 | /** 785 | * required int32 uid = 4; 786 | */ 787 | public Builder clearUid() { 788 | bitField0_ = (bitField0_ & ~0x00000008); 789 | uid_ = 0; 790 | onChanged(); 791 | return this; 792 | } 793 | 794 | private com.google.protobuf.ByteString uploadFile_ = com.google.protobuf.ByteString.EMPTY; 795 | /** 796 | * required bytes upload_file = 5; 797 | */ 798 | public boolean hasUploadFile() { 799 | return ((bitField0_ & 0x00000010) == 0x00000010); 800 | } 801 | /** 802 | * required bytes upload_file = 5; 803 | */ 804 | public com.google.protobuf.ByteString getUploadFile() { 805 | return uploadFile_; 806 | } 807 | /** 808 | * required bytes upload_file = 5; 809 | */ 810 | public Builder setUploadFile(com.google.protobuf.ByteString value) { 811 | if (value == null) { 812 | throw new NullPointerException(); 813 | } 814 | bitField0_ |= 0x00000010; 815 | uploadFile_ = value; 816 | onChanged(); 817 | return this; 818 | } 819 | /** 820 | * required bytes upload_file = 5; 821 | */ 822 | public Builder clearUploadFile() { 823 | bitField0_ = (bitField0_ & ~0x00000010); 824 | uploadFile_ = getDefaultInstance().getUploadFile(); 825 | onChanged(); 826 | return this; 827 | } 828 | 829 | // @@protoc_insertion_point(builder_scope:Upload) 830 | } 831 | 832 | static { 833 | defaultInstance = new Upload(true); 834 | defaultInstance.initFields(); 835 | } 836 | 837 | // @@protoc_insertion_point(class_scope:Upload) 838 | } 839 | 840 | private static final com.google.protobuf.Descriptors.Descriptor 841 | internal_static_Upload_descriptor; 842 | private static 843 | com.google.protobuf.GeneratedMessage.FieldAccessorTable 844 | internal_static_Upload_fieldAccessorTable; 845 | 846 | public static com.google.protobuf.Descriptors.FileDescriptor 847 | getDescriptor() { 848 | return descriptor; 849 | } 850 | private static com.google.protobuf.Descriptors.FileDescriptor 851 | descriptor; 852 | static { 853 | java.lang.String[] descriptorData = { 854 | "\n webapp/static/proto/upload.proto\"_\n\006Up" + 855 | "load\022\020\n\010filename\030\001 \002(\t\022\021\n\tcurrchunk\030\002 \002(" + 856 | "\005\022\016\n\006chunks\030\003 \002(\005\022\013\n\003uid\030\004 \002(\005\022\023\n\013upload" + 857 | "_file\030\005 \002(\014" 858 | }; 859 | com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = 860 | new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() { 861 | public com.google.protobuf.ExtensionRegistry assignDescriptors( 862 | com.google.protobuf.Descriptors.FileDescriptor root) { 863 | descriptor = root; 864 | return null; 865 | } 866 | }; 867 | com.google.protobuf.Descriptors.FileDescriptor 868 | .internalBuildGeneratedFileFrom(descriptorData, 869 | new com.google.protobuf.Descriptors.FileDescriptor[] { 870 | }, assigner); 871 | internal_static_Upload_descriptor = 872 | getDescriptor().getMessageTypes().get(0); 873 | internal_static_Upload_fieldAccessorTable = new 874 | com.google.protobuf.GeneratedMessage.FieldAccessorTable( 875 | internal_static_Upload_descriptor, 876 | new java.lang.String[] { "Filename", "Currchunk", "Chunks", "Uid", "UploadFile", }); 877 | } 878 | 879 | // @@protoc_insertion_point(outer_class_scope) 880 | } 881 | --------------------------------------------------------------------------------