如何在上传文件时为文件名添加时间戳

nan*_*ani 5 html javascript java html5 jsp

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

 }
Run Code Online (Sandbox Code Playgroud)
<html>

<body>

  <form action="UploadFile" method="post" enctype="multipart/form-data">
    Selectfile:
    <input type="file" name="filename">
    <br/>
    <input type="submit" value="Upload">
  </form>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

小智 0

获取当前日期和时间,并在上传时将其附加到您的文件名中。

private  final static String getDateTime()
{
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss");
    df.setTimeZone(TimeZone.getTimeZone("GMT")); // mention your timezone
    return df.format(new Date());
}
Run Code Online (Sandbox Code Playgroud)

现在将返回的字符串附加到文件名中。