JavaWeb项目实现文件上传动态显示进度

1 import java.io.File;
2 import java.io.FileOutputStream;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.List;
6
7 import javax.servlet.ServletException;
8 import javax.servlet.annotation.WebServlet;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;12
13 import org.apache.tomcat.util.http.fileupload.FileItem;
14 import org.apache.tomcat.util.http.fileupload.FileUploadException;
15 import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
16 import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;17 import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;
18
19 import singleton.ProgressSingleton;
20
21 @WebServlet("/UploadServlet")
22 public class UploadServlet extends HttpServlet {
23    private static final long serialVersionUID = 1L;
24
25    public UploadServlet() {
26     }
27
28    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
29       
30        DiskFileItemFactory factory = new DiskFileItemFactory();
31        factory.setSizeThreshold(4*1024);
32       
33        ServletFileUpload upload = new ServletFileUpload(factory);
34       
35        List<FileItem> fileItems;
36        try {
37            fileItems = upload.parseRequest(new ServletRequestContext(request));
38            //获取文件域
39            FileItem fileItem = fileItems.get(0);
40            //使用sessionid + 文件名生成文件号
41            String id = request.getSession().getId() + fileItem.getName();
42            //向单例哈希表写入文件长度和初始进度
43            ProgressSingleton.put(id + "Size", fileItem.getSize());
44            //文件进度长度
45            long progress = 0;
46            //用流的方式读取文件,以便可以实时的获取进度
47            InputStream in = fileItem.getInputStream();
48            File file = new File("D:/test");
49             file.createNewFile();
50            FileOutputStream out = new FileOutputStream(file);
51            byte[] buffer = new byte[1024];
52            int readNumber = 0;
53            while((readNumber = in.read(buffer)) != -1){
54                //每读取一次,更新一次进度大小
55                progress = progress + readNumber;
56                //向单例哈希表写入进度
57                ProgressSingleton.put(id + "Progress", progress);
58                 out.write(buffer);
59             }
60            //当文件上传完成之后,从单例中移除此次上传的状态信息
61            ProgressSingleton.remove(id + "Size");
62            ProgressSingleton.remove(id + "Progress");
63             in.close();
64             out.close();
65        } catch (FileUploadException e) {
66             e.printStackTrace();
67         }
68       
69        response.getWriter().print("done");
70     }
71
72    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
73         doGet(request, response);
74     }
75
76 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/d7986233506789a2c69c40465734d8f3.html