首页 > 代码库 > servlet3.0文件上传功能

servlet3.0文件上传功能

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.setCharacterEncoding("utf-8");
//获取文件部件part
Part part = request.getPart("file");
String h = part.getHeader("content-disposition");
String filename = h.substring(h.lastIndexOf("=") + 2, h.length() - 1);
//获取文件服务器头部信息
String root = request.getServletContext().getRealPath("/upload");
File f=new File(root);
f.mkdir();
part.write(root+"/"+filename);

}

 

serlvet中post方法

注意 jsp页面中file选择 的要有属性 name=‘file‘