servlet3.1文件上传
参考:https://www.cnblogs.com/xdp-gacl/p/4224960.html
页面
<body>
<p>${msg}</p>
<form action="UploadServlet" method="post" enctype="multipart/form-data">
name:<input type="text" name="name"/><br>
email:<input type="text" name="email"/><br>
上传文件: <input type="file" name="f1"> <br>
<input type="submit" value="上传"><br>
</form>
</body>
后台
package com.neuedu.controller;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
/**
* Servlet implementation class UploadServlet
*/
@WebServlet("/UploadServlet")
@MultipartConfig
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UploadServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//设置存储路径
//生产环境
//String savePath = request.getServletContext().getRealPath("/upload");
//开发环境
String savePath = "D:/apache-tomcat-8.0.36/wtpwebapps/upload";
String name=request.getParameter("name");
Part part = request.getPart("f1");
//自定义编写解析字符串的方法getFileName,获取文件名
String oriFileName = part.getSubmittedFileName();//调用获取文件名的方法
if(oriFileName.endsWith("JPG") || oriFileName.endsWith("PNG") || oriFileName.endsWith("jpg")|| oriFileName.endsWith("png")) {
//获取原文件名后缀
String suffix = oriFileName.substring(oriFileName.lastIndexOf("."));
//避免上传后文件名称重复,使用uuid文件名
String fileName=UUID.randomUUID().toString().replaceAll("-", "");
fileName = fileName + suffix;
// 把文件写到指定路径
part.write(savePath + File.separator +fileName);
PrintWriter out = response.getWriter();
request.setAttribute("msg","上传成功");
request.getRequestDispatcher("upload.jsp").forward(request, response);
}else {
request.setAttribute("msg","请上传图片类型的文件!");
request.getRequestDispatcher("upload.jsp").forward(request, response);
}
}
}
解决重启服务图片丢失的问题
总体是通过配置tomcat虚拟目录来实现
0)在D:\apache-tomcat-8.0.36\wtpwebapps下新建upload目录
1)开发环境,文件上传时,传到D:\apache-tomcat-8.0.36\wtpwebapps\upload目录下。
2)eclipse的servers下找到server.xlml
增加如下配置:
<Context docBase="upload" path="/java1/upload" reloadable="true" source="org.eclipse.jst.jee.server:java1"/>
3)测试
http://localhost:8081/java1/upload/430107639a9f476fa0f6a2ee3b48193e.png