文件上传案例
- 朋友圈 ,邮箱上传附件.百度云盘 360云盘.
- 代码实现过程:
- 客户端要求:
- 要求form表单必须有一个文件上传项
- 请求的方式必须为post 因为get提交数据有大小的限制
- 必须加上一个enctype = multipart/form-data 否则请求体里面没有内容
- 服务器逻辑:
@MultipartConfig @WebServlet("/uploads") public class Upload extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); Part part = req.getPart("file"); String realPath = getServletContext().getRealPath("fileUpload"); System.out.println(realPath); String header = part.getHeader("Content-Disposition"); String[] array = header.split(";"); String filename = array[array.length - 1]; String[] arr = filename.split("="); String filerealname = arr[arr.length - 1]; String file = filerealname.substring(1, filerealname.length() - 1); System.out.println(file); System.out.println(realPath + "\\" + file); part.write(realPath + "\\" + file); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
原文地址:https://www.cnblogs.com/nangongyibin/p/10225922.html
时间: 2024-10-04 18:51:17