MyException - 我的异常网
当前位置:我的异常网» Java Web开发 » 获得上传文件名的例子

获得上传文件名的例子(3)

www.MyException.Cn  网友分享于:2015-08-27  浏览:617次

DynaActionForm upForm = (DynaActionForm) form;
String dir = this.getServlet().getServletContext().getRealPath( "/upload/uploaded ");
FormFile fileOne = (FormFile)upForm.get( "file1 ");
FormFile fileTwo = (FormFile)upForm.get( "file2 ");
FormFile fileThree = (FormFile)upForm.get( "file3 ");
FormFile fileFour = (FormFile)upForm.get( "file4 ");
FormFile[] files = new FormFile[]{fileOne, fileTwo, fileThree, fileFour};
List fileList = new ArrayList();
for(int i = 0;i <files.length;i++){
if(files[i] == null)
continue;
String fileName = files[i].getFileName();
int size = files[i].getFileSize();
try {
InputStream streamIn = files[i].getInputStream();
OutputStream streamOut = new FileOutputStream(dir + "/ " + fileName);
int bytesRead = 0;
byte[] buffer = new byte[4096];
while((bytesRead = streamIn.read(buffer, 0, 4096)) != -1)
streamOut.write(buffer, 0, bytesRead);
streamOut.close();
streamIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
ActionMessages msgs = new ActionMessages();
ActionMessage msg = new ActionMessage( "error.upload.fail ");
msgs.add( "error.upload.fail ",msg);
request.setAttribute(Globals.MESSAGE_KEY,msgs);
} catch (IOException e) {
e.printStackTrace();
ActionMessages msgs = new ActionMessages();
ActionMessage msg = new ActionMessage( "error.upload.fail ");
msgs.add( "error.upload.fail ",msg);
request.setAttribute(Globals.MESSAGE_KEY,msgs);
}
fileList.add(fileName);
ActionMessages msgs = new ActionMessages();
ActionMessage msg = new ActionMessage( "error.upload.success ");
msgs.add( "error.upload.success ",msg);
request.setAttribute(Globals.MESSAGE_KEY,msgs);
}


文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有