struts如何实现上传文件的重命名???? OutputStream streamOut = new FileOutputStream(dir + "/"+fname);fname 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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")); } 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"); } ssh2,启动报错,求助了 有没有谁有孙鑫老师的j2ee视频教程呀 manytoone的简单问题 为什么我没法使用beanUtils啊啊 JSP学习方法求助 在spring里一个service 类中有多个dao,是否会影响性能? 联创软件java工程师笔试考什么东西? jdbc与SQL Server问题 共同翻译:Designing Enterprise Applications with the J2EETM Platform,第二版? Java同步块问题 请问websphere部署的一个问题。部署的应用怎么找不到了? 某大型软件公司招聘 JAVA软件工程师
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"));
}
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"); }