文件上传&下载

喜欢ヅ旅行 2022-05-19 05:28 510阅读 0赞

上传步骤
1、创建解析工厂DiskFileItemFactory对象
2、使用DiskFileItemFactory 对象创建ServletFileUpload对象。
3、调用ServletFileUpload.parseRequest方法解析request对象,得到一个保存了所有上传内容的List对象。
4、对list进行迭代,每迭代一个FileItem对象,调用其isFormField方法判断是否为附件
5、普通表单字段,则调用getFieldName、getString方法得到字段名和字段值
6、文件,则调用getInputStream方法得到数据输入流,从而读取上传数据。

下载步骤
1.设置文件ContentType类型 “multipart/form-data“
2.设置文件头 “Content-Disposition”, “attachment;fileName=文件名”
3.通过response获取ServletOutputStream对象(out)
4.通过输出流(out)向浏览器响应文件

login.jsp设置view视图(附部分关键代码):

  1. <body>
  2. <form action="upload01" method="post" enctype="multipart/form-data">
  3. <p>用户名:<input type="text" name="username" /></p>
  4. <p>选择您要上传的文件:<input type="file" name="file" /></p>
  5. <p>上传<input type="submit" value="上传" /></p>
  6. </form>
  7. </body>

controller包下servlet文件UploadServlet01.java,进行文件上传控制(仅附关键代码)
doPost中写入:

  1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  2. InputStream is=null;
  3. FileOutputStream fos=null;
  4. FileItemFactory factory=new DiskFileItemFactory();
  5. ServletFileUpload upload=new ServletFileUpload(factory);
  6. upload.setHeaderEncoding("utf-8");
  7. try {
  8. List<FileItem> list=upload.parseRequest(request);
  9. for(FileItem li:list) {
  10. //判断是否文本标签
  11. if(li.isFormField()) {
  12. String field=li.getFieldName();//获取标签名
  13. String value=li.getString();//获取标签值
  14. System.out.println(field+":"+value);
  15. }else {
  16. String filename=li.getName();//获取上传文件名
  17. //文件输入流,读取数据
  18. is=li.getInputStream();
  19. //String path="D:\\upload";//保存在本地D盘下
  20. String path = this.getServletContext().getRealPath("/upload/");//保存在Tomcat目录下
  21. File file=new File(path);
  22. if(!file.exists()) {
  23. file.mkdirs();//创建目录
  24. //file.createNewFile();此不创建文件,是因为FileOutputStream会自动帮创建文件而不创建目录
  25. }
  26. fos=new FileOutputStream(path+"\\"+filename);
  27. int i=-1;
  28. byte[] b=new byte[1024];
  29. while((i=is.read(b))!=-1) {
  30. fos.write(b,0,i);//i为向字节数组中写入字节的个数
  31. }
  32. }
  33. }
  34. } catch (FileUploadException e) {
  35. e.printStackTrace();
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. } finally {
  39. if(is!=null) {
  40. is.close();}
  41. if(fos!=null) {fos.close();}
  42. }
  43. }

download.jsp,前端视图显示:

  1. <body>
  2. <a href="downLoad?filename=素质拓展流程.png">素质拓展流程.png</a>
  3. </body>

controller包下servlet文件downloadServlet01.java,进行文件下载控制(仅附关键代码)
doPost中写入:

  1. /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
  2. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  3. doGet(request, response);
  4. String fname=request.getParameter("filename");
  5. String path=this.getServletContext().getRealPath("/upload");
  6. String filename=path+"/"+fname;//filename为全路径
  7. //设置响应头
  8. response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fname,"utf-8"));
  9. //获取ServletOutputStream对象
  10. ServletOutputStream out=response.getOutputStream();
  11. //读文件
  12. FileInputStream fis=new FileInputStream(filename);
  13. int i=-1;
  14. byte[] b=new byte[1024];
  15. while((i=fis.read(b))!=-1) {
  16. out.write(b,0,i);
  17. }
  18. fis.close();
  19. out.close();
  20. }

success.jsp,下载后回显:

  1. <body>
  2. <a href="downLoad?filename=素质拓展流程.png">素质拓展流程.png</a>
  3. </body>

发表评论

表情:
评论列表 (有 0 条评论,510人围观)

还没有评论,来说两句吧...

相关阅读