Struts2上传图片到文件图片上传

lai 贡献于2012-04-17

作者 USER  创建于2010-07-27 03:44:00   修改者USER  修改于2010-07-27 03:44:00字数5221

文档摘要:Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。
关键词:

实现原理 Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。 需要的包 jsp页面 FileUpload.jsp: <% @ page language = " java " contentType = " text/html; charset=utf-8 " pageEncoding = " utf-8 " %> <% @ taglib prefix = " s " uri = " /struts-tags " %> < html xmlns ="http://www.w3.org/1999/xhtml" > < head > < title > Struts 2 File Upload < body > < form action ="fileUpload" method ="POST" enctype ="multipart/form-data" > < file name ="file" ContentEditable="false"/> 备注:form中enctype ="multipart/form-data"是必须的,莫漏, ContentEditable="false"是用来禁止用户在浏览地址框里进行手动输入的。 Action package tutorial; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Date; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends ActionSupport { private static final long serialVersionUID = 572146812454l ; private static final int BUFFER_SIZE = 16 * 1024 ; private File myFile; private String contentType; private String fileName; private String imageFileName; private String caption; public void setMyFileContentType(String contentType){ this .contentType = contentType; } public void setMyFileFileName(String fileName) { this .fileName = fileName; } public void setMyFile(File myFile) { this .myFile = myFile; } public String getImageFileName() { return imageFileName; } public String getCaption() { return caption; } public void setCaption(String caption) { this .caption = caption; } private static void copy(File src, File dst) { try { InputStream in = null ; OutputStream out = null ; try { in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE); out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE); byte [] buffer = new byte [BUFFER_SIZE]; while (in.read(buffer) > 0 ) { out.write(buffer); } } finally { if ( null != in) { in.close(); } if ( null != out) { out.close(); } } } catch (Exception e) { e.printStackTrace(); } } private static String getExtention(String fileName) { int pos = fileName.lastIndexOf( " . " ); return fileName.substring(pos); } @Override public String execute() { imageFileName = new Date().getTime() + getExtention(fileName); File imageFile = new File(ServletActionContext.getServletContext().getRealPath( " /UploadImages " ) + " / " + imageFileName); copy(myFile, imageFile); return SUCCESS; } } 备注: myFile,还有myFileContentType(上传文件的MIME类型)和myFileFileName和jsp页面中的file标签对应 显示页面 <% @ page language = " java " contentType = " text/html; charset=utf-8 " pageEncoding = " utf-8 " %> <% @ taglib prefix = " s " uri = " /struts-tags " %> < html xmlns ="http://www.w3.org/1999/xhtml" > < head > < title > Struts 2 File Upload < body > < div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" > < img src ='UploadImages/ ' /> < br /> < s:property value ="caption" /> Action的配置文件 < struts > < package name ="fileUploadDemo" extends ="struts-default" > < action name ="fileUpload" class ="tutorial.FileUploadAction" > < interceptor-ref name ="fileUploadStack" /> < result name ="success" > /ShowUpload.jsp web.xml配置文件 < web-app id ="WebApp_9" version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > < display-name > Struts 2 Fileupload < filter > < filter-name > struts-cleanup < filter-class > org.apache.struts2.dispatcher.ActionContextCleanUp < filter > < filter-name > struts2 < filter-class > org.apache.struts2.dispatcher.FilterDispatcher < filter-mapping > < filter-name > struts-cleanup < url-pattern > /* < filter-mapping > < filter-name > struts2 < url-pattern > /* < welcome-file-list > < welcome-file > index.html 阻止用户上传非图片类型的文件 方法一: 修改FileUpload.jsp,在与之间加入“”,用于在页面上输出错误信息。 然后修改struts.xml文件,将Action fileUpload改为如下所示: < action name ="fileUpload" class ="tutorial.FileUploadAction" > < interceptor-ref name ="fileUpload" > < param name ="allowedTypes" > image/bmp,image/png,image/gif,image/jpeg < interceptor-ref name ="defaultStack" /> < result name ="input" > /FileUpload.jsp < result name ="success" > /ShowUpload.jsp 方法二: BufferedImage imgBuffer = ImageIO.read(file); if(null == imgBuffer) { // 非图片,进行处理 } 阻止用户上传图片过大 方法一:配置fileupload拦截器的maximumSize参数(设定能接受的文件的最大长度) < param name ="fileUpload.maximumSize" > 102400 方法二:在struts.xml文件中添加 (限制上传文件的最大长度) 关于将图片上传到项目 用ServletActionContext.getServletContext().getRealPath(str);来获取项目地址,其中str是项目下的相对路径 在eclipse 下上传图片到项目,即使上传成功也看不到,因为它被存储在一个临时目录下,你可以在你的项目文件下搜一下 只有打成var包发布后才能在项目中实际存储 未解决问题 一、不能实现在jsp中控制上传文件的大小 二、若JPEG图片的ICC信息被破坏了ImageIO会抛出异常bandOffsets.length is wrong! 欢迎大家提出问题,更欢迎大家解决上面两个问题

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ] 3 人已下载

下载文档