How to add a timestamp for a file name when uploading a file

This is the associated Java code to download the file code, and I need to add a timestamp for the file name, and then it will be loaded into a specific directory

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()); } } 
 <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> 
+5
source share
3 answers

MultipartRequest contains a file rename policy by default.

To avoid collisions and have excellent control over the file allocation, there is a kind of constructor that implements a plug-in implementation of FileRenamePolicy. A specific policy may choose to rename or relocate a file before writing it.

 MultipartRequest(javax.servlet.http.HttpServletRequest request, java.lang.String saveDirectory, int maxPostSize, FileRenamePolicy policy) 

Note. Due to the low reputation, I can not add comments and had to make this contribution as an answer. Do not reduce it; instead, correct or comment on the same.

+3
source

Just enter "_" + System.currentTimeMillis() in the file name?

If instead of milliseconds you need an intelligent timestamp, just use DateFormat as shown in another answer.

With Java EE> = 6:

 @WebServlet("/FileUploadServlet") @MultipartConfig(fileSizeThreshold=1024*1024*10, // 10 MB maxFileSize=1024*1024*50, // 50 MB maxRequestSize=1024*1024*100) // 100 MB public class FileUploadServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String applicationPath = request.getServletContext().getRealPath(""); String uploadFilePath = applicationPath + File.separator + "uploads"; File fileSaveDir = new File(uploadFilePath); if (!fileSaveDir.exists()) { fileSaveDir.mkdirs(); } String fileName = null; for (Part part : request.getParts()) { fileName = getFileName(part) + "_" + System.currentTimeMillis(); // <----- HERE part.write(uploadFilePath + File.separator + fileName); } request.setAttribute("message", fileName + " File uploaded successfully!"); getServletContext().getRequestDispatcher("/response.jsp").forward( request, response); } private String getFileName(Part part) { String contentDisp = part.getHeader("content-disposition"); String[] tokens = contentDisp.split(";"); for (String token : tokens) { if (token.trim().startsWith("filename")) { return token.substring(token.indexOf("=") + 2, token.length()-1); } } return ""; } } 

The code is fork of one of this article.

+1
source

Get the current date and time and add it to the file name at boot time.

 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()); } 

Now add the returned string to the file name.

0
source

All Articles