这是上传文件代码的相关java代码,我需要为文件名添加时间戳,然后将其上传到特定目录
public class Upload extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException {
System.out.println(this.getClass().getName());
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
//boolean MultipartRequest;
//String PrintWriter;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
MultipartRequest multipartRequest = new MultipartRequest(request,"/home/hadoop/Desktop");
out.println("succcesfully uploaded");
}
public void destroy() {
System.out.println(this.getClass().getName());
}
}
最佳答案
默认情况下,MultipartRequest包含文件重命名策略.
原文链接:https://www.f2er.com/html/426146.htmlTo avoid collisions and have fine control over file placement,there’s a constructor variety that takes a pluggable FileRenamePolicy implementation. A particular policy can choose to rename or change the location of the file before it’s written.
MultipartRequest(javax.servlet.http.HttpServletRequest request,java.lang.String saveDirectory,int maxPostSize,FileRenamePolicy policy)