java – 如何在上载文件时为文件名添加时间戳

前端之家收集整理的这篇文章主要介绍了java – 如何在上载文件时为文件名添加时间戳前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这是上传文件代码的相关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());
   }

 }
@H_404_14@最佳答案
默认情况下,MultipartRequest包含文件重命名策略.

To 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)

注意:由于声誉低,我无法添加评论,不得不作为答案贡献.不要低估这一点,而是纠正或评论.

原文链接:https://www.f2er.com/html/426146.html

猜你在找的HTML相关文章