好像最低支持3.0的JavaWeb项目;
前台页面代码:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="GBK"> 5 <title>测试上传</title> 6 </head> 7 <body> 8 <form action="/fileup/upTest" enctype="multipart/form-data" 9 method="post"> 10 11 <input type="file" name="file01"> <input type="submit" 12 value="UP"> 13 14 </form> 15 </body> 16 </html>
后台:
1 import java.io.IOException; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.MultipartConfig; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.Part; 9 10 @MultipartConfig // 必须设置 11 public class UpTest extends HttpServlet { 12 13 private static final long serialVersionUID = 1L; 14 15 protected void doPost(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 Part part = request.getPart("file01");// html表单中file的neme属性值。 18 String header = part.getHeader("Content-Disposition"); 19 int lastIndexOf = header.lastIndexOf("=");// 获取文件名 20 header = header.substring(lastIndexOf, header.length()); 21 header = header.substring(2, header.length() - 1); 22 part.write("F:/" + header);// 保存文件 23 24 } 25 26 }
最简单的实现上传文件,不使用框架,。上传文件大小可以在注解的参数里面设置;
原文地址:https://www.cnblogs.com/miss3316/p/8484913.html
时间: 2024-10-29 07:36:25