This is a related java code for upload file code and i need to add a timestamp for a file name and then it is uploaded to the particular 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>
MultipartRequest by default contains a file rename policy.
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)
Note: Due to low reputation, I can't add comments and had to contribute this as an answer. Don't downvote this, instead correct or comment on the same.
Simply concat "_" + System.currentTimeMillis()
to the filename ?
If instead of the milliseconds you want the intellegible timestamp, simply use a DateFormat as shown in the other 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 a fork of the one in this article
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With