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");
}
}
}
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");
}
}
}
相关文章推荐
- itcast张孝祥struts文件上传单元笔记
- Struts学习笔记: 实现文件上传下载
- Struts2————学习笔记6 文件的上传与下载 以及AJAX
- Struts2.1笔记(九):上传和下载文件
- Struts2.0学习笔记---上传文件和多文件上传
- Struts 1 学习笔记-4-1(Struts动态表单的测试以及上传文件的应用)
- Struts中文件的上传和下载-笔记
- struts文件上传、文件下载
- java如何保存上传后的文件 (struts2与struts1)
- PHP开发笔记——文件的上传下载
- 文件上传下载学习笔记
- 在Struts 2中实现文件上传
- 在LINUX下,struts 2 文件上传路径问题
- Struts 2 实现上传文件
- struts(八)文件上传
- 用Struts上传多个文件的方法
- Struts 2 之文件上传
- Struts2单文件、多文件上传
- JSP学习笔记:通过UUID为解决中文文件上传后的命名问题
- struts上传文件的方法