Java 上传文件时如何为文件名添加时间戳
这是上传文件代码的相关java代码,我需要为文件名添加时间戳,然后将其上传到特定目录Java 上传文件时如何为文件名添加时间戳,java,javascript,html,jsp,Java,Javascript,Html,Jsp,这是上传文件代码的相关java代码,我需要为文件名添加时间戳,然后将其上传到特定目录 public class Upload extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { System.out.println(this.getClass().getName()); } p
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());
}
}
选择文件:
获取当前日期和时间,并在上载时将其附加到文件名中
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());
}
现在将返回的字符串附加到文件名中。只需将concat““+System.currentTimeMillis()
添加到文件名中即可
如果不需要毫秒,而需要智能时间戳,只需使用另一个答案中所示的DateFormat
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 "";
}
}
@WebServlet(“/FileUploadServlet”)
@MultipartConfig(fileSizeThreshold=1024*1024*10,//10 MB
maxFileSize=1024*1024*50,//50MB
maxRequestSize=1024*1024*100)//100MB
公共类FileUploadServlet扩展了HttpServlet{
受保护的void doPost(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
字符串applicationPath=request.getServletContext().getRealPath(“”);
字符串uploadFilePath=applicationPath+File.separator+“uploads”;
File fileSaveDir=新文件(上传文件路径);
如果(!fileSaveDir.exists()){fileSaveDir.mkdirs();}
字符串文件名=null;
for(部分:request.getParts()){
fileName=getFileName(part)+“”+System.currentTimeMillis();//默认情况下包含文件重命名策略
为了避免冲突并对文件位置有良好的控制,有一种构造函数采用可插入的FileRenamePolicy实现。特定的策略可以选择在写入文件之前重命名或更改文件的位置
注意:由于声誉不好,我无法添加评论,只能将此作为答案。不要否决此内容,而是更正或对其进行评论。您想要时间戳作为文件名,是吗?获取时间戳
,然后制作一个带有samename\u时间戳
的文件,并上载这两个文件。或者在末尾添加一行在file@AlokMishra我需要相同的时间戳。extention@UmaKanth:您肯定是指yyyyMM…格式的timestamp\u filename?在我的代码中,我需要在其中添加此“\u”+System.currentTimeMillis()您的代码没有做任何事情…如果您想自己编写它,然后使用我的答案中的代码;如果您想使用ORILLY包装器,那么使用@ GopAL的答案,否则考虑使用比servlet更进化的东西,比如框架(例如Struts2)。
MultipartRequest(javax.servlet.http.HttpServletRequest request,
java.lang.String saveDirectory,
int maxPostSize,
FileRenamePolicy policy)