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>
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>
Share
Improve this question
edited Jun 12, 2015 at 10:06
R3tep
12.9k10 gold badges51 silver badges76 bronze badges
asked Jun 12, 2015 at 10:03
naninani
1652 silver badges12 bronze badges
4
- you want timestamp as the File name , is it so ? – M Alok Commented Jun 12, 2015 at 10:12
-
get the
timestamp
then make a file with thesamename_timestamp
and upload both of them. Or add a line at the end within the file – Uma Kanth Commented Jun 12, 2015 at 10:14 - @Alok Mishra i need samefilename_timestamp.extention – nani Commented Jun 12, 2015 at 11:09
- @UmaKanth: Surely you mean timestamp_filename? In yyyyMM... format. – jmoreno Commented Jun 12, 2015 at 14:39
3 Answers
Reset to default 3MultipartRequest 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 ments and had to contribute this as an answer. Don't downvote this, instead correct or ment 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
Get the current date and time and append it to your file name while uploading.
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 append the returned string in the filename.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745445844a4628043.html
评论列表(0条)