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.
source share