您的位置:首页 > 编程语言 > Java开发

Struts上传文件笔记

2006-06-30 11:01 363 查看
<html:file property="picture"/>

FormFile formFile = form.getPicture();
String picName = formFile.getFileName();
if ( picName != null && picName.length() != 0 ) {
String temp = picName.substring(picName.indexOf("."), picName.length()).toLowerCase();
if ( !(".jpg".equalsIgnoreCase(temp)||".gif".equalsIgnoreCase(temp)||".jpeg".equalsIgnoreCase(temp))) {
ActionErrors errors = new ActionErrors();
errors.add( ActionMessages.GLOBAL_MESSAGE, new ActionMessage("enterprise.error.filetype"));
this.saveErrors(httpServletRequest, errors);
return actionMapping.getInputForward();
}
else {
try {
InputStream is = formFile.getInputStream();
OutputStream out = new FileOutputStream (new StringBuffer(httpServletRequest.getRealPath("/")).append("/jsp/magazine/images/").append(picName).toString());
int bytesRead = 0 ;
byte[] buffer = new byte[8192];
while ( (bytesRead = is.read(buffer,0,8192)) != -1 ) {
out.write(buffer,0,bytesRead);
}
out.close();
is.close();
}
catch ( FileNotFoundException fileNotFoundException ) {
log.error(fileNotFoundException );
return actionMapping.findForward("error");
}
catch ( IOException ioException ) {
log.error(ioException );
return actionMapping.findForward("error");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: