加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

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

发布时间:2020-12-14 22:56:30 所属栏目:资源 来源:网络整理
导读:这是上传文件代码的相关java代码,我需要为文件名添加时间戳,然后将其上传到特定目录 public class Upload extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { System.out.println(this.

这是上传文件代码的相关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包含文件重命名策略.

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)

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读