struts如何实现上传文件的重命名????
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String dir=servlet.getServletContext().getRealPath("/upload");
HtmlFileForm hff = (HtmlFileForm) form;
// org.apache.struts.upload.FormFile contains the uploaded file
FormFile file = hff.getFile();
// If no file was uploaded (e.g. first form load), then display View
if (file == null ) {
return mapping.findForward("success");
}
// Get the name and file size
String fname = file.getFileName();
String size = Integer.toString(file.getFileSize()) + " bytes";
InputStream streamIn = file.getInputStream();
OutputStream streamOut = new FileOutputStream(dir + "/"+fname);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
// Populate the form bean with the results for display in the View
hff.setFname(fname);
hff.setSize(size);
// Clean up our toys when done playing
file.destroy();
// Forward to default display
return mapping.findForward("success");
}
------------------------------------------------------------------------------------
以上代码是孙卫琴的《精通struts:基于mvc的java web设计与开发》中关于文件上传的列子,偶的想法是
,把上传的文件要根据当前系统时间进行重命名后在传到相应的upload目录。
例如:现在是2006年03月06日14:23:21,用户通过浏览指定某一个目录下的a.txt文件,当用户点上传按
纽后,上传到服务器的upload目录下,就成了20060306142321.txt这个文件,请问在上边的方法中该怎么
实现,请路过的高人朋友说一下。谢谢。
问题点数:100、回复次数:7Top
1 楼jfy3d(剑事 http://www.migti.com)回复于 2006-03-06 16:34:16 得分 0
OutputStream streamOut = new FileOutputStream(dir + "/"+fname);
fnameTop
2 楼li_d_s(鄙视那些不懂Java却跑来乱骂的人,.NET没啥了不起)回复于 2006-03-06 16:59:17 得分 0
FormFile file = noticeEditForm.getAttachFile();//取得上传的文件
if(null != file && null != file.getFileName() && 0 != file.getFileName().trim().length()){
String fileName = Feedback.getFormatedCurrentDate(conn) + staff.getId() + StringUtil.getExtendName(file.getFileName());
try {
InputStream stream = file.getInputStream();//把文件读入
String filePath = servlet.getServletContext().getRealPath("/");//取当前系统路径
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(filePath + "attachment\\" + fileName);//建立一个上传文件的输出流.
notice.setIsAnnex("1");
notice.setFilePath("./attachment/" + fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);//将文件写入服务器
}
bos.close();
stream.close();
}catch(Exception e){
errors.add("uploadFail", new ActionError("error.bulletin.uploadFail"));
e.printStackTrace();
}
}else{
//errors.add("fileRequired", new ActionError("error.bulletin.fileRequired"));
}Top
3 楼li_d_s(鄙视那些不懂Java却跑来乱骂的人,.NET没啥了不起)回复于 2006-03-06 17:00:23 得分 0
代码有点乱,就是把上传的文件读出来写到新的你命名的文件Top
4 楼jeffaple(坤)回复于 2006-03-06 17:08:21 得分 0
路过,学习,我还没看到这边。。。Top
5 楼zhh1981(**的猪头)回复于 2006-03-06 17:14:39 得分 100
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String dir=servlet.getServletContext().getRealPath("/upload");
HtmlFileForm hff = (HtmlFileForm) form;
// org.apache.struts.upload.FormFile contains the uploaded file
FormFile file = hff.getFile();
// If no file was uploaded (e.g. first form load), then display View
if (file == null ) {
return mapping.findForward("success");
}
// Get the name and file size
String fname = file.getFileName();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
java.util.Date mydate=new java.util.Date();
String s=sdf.format(mydate);
fname = s + fname .substring(fname.indexOf("."));
String size = Integer.toString(file.getFileSize()) + " bytes";
InputStream streamIn = file.getInputStream();
OutputStream streamOut = new FileOutputStream(dir + "/"+fname);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
// Populate the form bean with the results for display in the View
hff.setFname(fname);
hff.setSize(size);
// Clean up our toys when done playing
file.destroy();
// Forward to default display
return mapping.findForward("success");
}
Top
6 楼fasi123()回复于 2006-03-07 13:11:55 得分 0
楼上的朋友,代码好像不行啊。。。Top
7 楼fasi123()回复于 2006-03-07 13:17:32 得分 0
没错,是偶错怪楼上的高人啦。偶马上结帖。。。。。。Top




