【ServletFileUpLoad】上传本地文件到服务器

举报
brucexiaogui 发表于 2021/12/30 02:31:20 2021/12/30
【摘要】 【ServletFileUpLoad】上传本地文件到服务器 一、项目需要的jar包 二、上传实例 package test23_2; import java.io.File;import java.io.IOException;import java.io....
【ServletFileUpLoad】上传本地文件到服务器

一、项目需要的jar包


二、上传实例


   
  1. package test23_2;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.List;
  6. import java.util.UUID;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import org.apache.commons.fileupload.FileItem;
  12. import org.apache.commons.fileupload.FileUploadException;
  13. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  14. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  15. import org.apache.commons.io.FileUtils;
  16. /*
  17. * 实现基本上传并对文件进行UUID重命名
  18. */
  19. public class UploadServlet2 extends HttpServlet {
  20. public void doPost(HttpServletRequest request, HttpServletResponse response)
  21. throws ServletException, IOException {
  22. request.setCharacterEncoding("UTF-8");
  23. response.setContentType("text/html;charset=utf-8");
  24. PrintWriter out = response.getWriter();
  25. //1、设置临时上传的路径
  26. DiskFileItemFactory disk =
  27. new DiskFileItemFactory(10240,new File("e:disk"));
  28. //2、设置文件上传的目标路径
  29. String servePath = getServletContext().getRealPath("/serviceDisk");
  30. System.out.println(servePath+"-------------------------");
  31. //3、申明upload
  32. ServletFileUpload up = new ServletFileUpload(disk);
  33. //4、解析request
  34. try {
  35. List<FileItem> list = up.parseRequest(request);
  36. for(FileItem file:list){
  37. if(!file.isFormField()){
  38. //获取上传文件的名字
  39. String fileName = file.getName();
  40. fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
  41. //获取上传文件的后缀
  42. String extName = fileName.substring(fileName.lastIndexOf("."));
  43. //申明UUID
  44. String uuid = UUID.randomUUID().toString().replace("-", "");
  45. //组成新的名称
  46. String newName = uuid+extName;
  47. //上传文件
  48. FileUtils.copyInputStreamToFile(file.getInputStream(),
  49. new File(servePath+"/"+newName));
  50. //封装
  51. request.setAttribute("newName", newName);
  52. request.setAttribute("oldName", fileName);
  53. }
  54. }
  55. //转发
  56. request.getRequestDispatcher("jsps/show2.jsp").forward(request, response);
  57. } catch (FileUploadException e) {
  58. // TODO Auto-generated catch block
  59. new RuntimeException();
  60. }
  61. }
  62. }

三、前端选择上传文件实例



   
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'upload2.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <p>上传文件,重命名<p/> <br>
  22. <form action="UploadServlet2" method="post" enctype="multipart/form-data">
  23. 上传图片<input type="file" name="imgs"/></br>
  24. 图片说明<input type="text" name="node"/></br>
  25. <input type="submit" value="上传"/></br>
  26. </form>
  27. </body>
  28. </html>

四、前端显示上传文件结果信息的实例


   
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11. <title>My JSP 'show2.jsp' starting page</title>
  12. <meta http-equiv="pragma" content="no-cache">
  13. <meta http-equiv="cache-control" content="no-cache">
  14. <meta http-equiv="expires" content="0">
  15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  16. <meta http-equiv="description" content="This is my page">
  17. <!--
  18. <link rel="stylesheet" type="text/css" href="styles.css">
  19. -->
  20. </head>
  21. <body>
  22. 上传文件重命名UUID <br>
  23. 文件新名称 ${newName}</br>
  24. 文件上传名称 ${oldName}</br>
  25. <img src="<c:url value='/serviceDisk/${newName}'/>"/>
  26. </body>
  27. </html>

五、上传流程介绍

通过前端上传页面,用户可以选择文件上传,上传核心类接收到请求后,处理请求。上传成功后,将图片信息转发到显示图片信息的前端页面展示。

文章来源: brucelong.blog.csdn.net,作者:Bruce小鬼,版权归原作者所有,如需转载,请联系作者。

原文链接:brucelong.blog.csdn.net/article/details/78404684

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200