java 控制器在JSP / Servlet中上传单个文件 - https://ngockhuong.com
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 控制器在JSP / Servlet中上传单个文件 - https://ngockhuong.com相关的知识,希望对你有一定的参考价值。
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
/**
* author: Lam Ngoc Khuong
* https://ngockhuong.com
*/
@MultipartConfig(fileSizeThreshold=1024*1024*5, //5MB
maxFileSize=1024*1024*6, //6MB
maxRequestSize=1024*1024*7) // 7MB
public class UploadController extends HttpServlet {
private static final long serialVersionUID = 1L;
public UploadController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String status = request.getParameter("status");
final Part part = request.getPart("image");
// lấy tên file
String filename = getFileName(part);
// đường dẫn lưu file
final String path = request.getServletContext().getRealPath("/files");
System.out.println(path);
File dirPath = new File(path);
if (!dirPath.exists()) {
dirPath.mkdir();
}
// ghi file
String filePath = path + File.separator + filename;
part.write(filePath);
}
private String getFileName(final Part part) {
final String partHeader = part.getHeader("content-disposition");
for (String content : partHeader.split(";")) {
if (content.trim().startsWith("filename")) {
return content.substring(
content.indexOf('=') + 1).trim().replace("\"", "");
}
}
return null;
}
}
以上是关于java 控制器在JSP / Servlet中上传单个文件 - https://ngockhuong.com的主要内容,如果未能解决你的问题,请参考以下文章
servlet+jsp实现的文件上传与下载
jsp和servlet学习总结
java 控制器在JSP / Servlet中上传单个文件 - https://ngockhuong.com
Jsp与servlet的区别
Jsp与servlet的区别
Servlet基础