现在的位置: 首页 > 综合 > 正文

Struts2通过commons-fileupload实现文件上传

2013年08月03日 ⁄ 综合 ⁄ 共 3748字 ⁄ 字号 评论关闭

相应源代码:http://download.csdn.net/source/834246

upload.jsp

  1. <%@ page language="java" contentType="text/html; charset=GBK"%>
  2. <%@taglib prefix="s" uri="/struts-tags"%>
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
  6. <title>修改默认的提示信息</title>
  7. </head>
  8. <body>
  9. <div style="color:red">
  10. <!--采用红色的前景色,输出错误信息 -->
  11. <s:fielderror/>
  12. </div>
  13. <form action="upload.action" method="post" enctype="multipart/form-data">
  14.     文件标题:<input type="text" name="title" /><br>
  15.     选择文件:<input type="file" name="upload" /><br>
  16.     <input value="上传" type="submit" />
  17. </form>
  18. </body>
  19. </html>

succ.jsp

  1. <%@ page language="java" contentType="text/html; charset=GBK"%>
  2. <%@taglib prefix="s" uri="/struts-tags"%>
  3. <html>
  4.     <head>
  5.         <title>上传成功</title>
  6.     </head>
  7.     <body>
  8.         上传成功!<br>
  9.         文件标题:<s:property value=" + title"/><br>
  10.         文件为:<img src="<s:property value="'upload/' + uploadFileName"/>"/><br>
  11.     </body>
  12. </html>

web.xml

  1.     <!-- 定义Struts2的FilterDispathcer的Filter -->
  2.     <filter>
  3.         <filter-name>struts2</filter-name>
  4.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  5.     </filter>
  6.     <!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
  7.     <filter-mapping>
  8.         <filter-name>struts2</filter-name>
  9.         <url-pattern>/*</url-pattern>
  10.     </filter-mapping>

struts.xml

  1. <struts>
  2.     <constant name="struts.custom.i18n.resources" value="globalMessages"/>
  3.     <constant name="struts.i18n.encoding" value="GBK"/>
  4.     <package name="upload" extends="struts-default">
  5.         <action name="upload" class="jCuckoo.UploadAction">
  6.             <interceptor-ref name="fileUpload"> 
  7.                 <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param> 
  8.                 <param name="maximumSize">20000</param> 
  9.             </interceptor-ref> 
  10.             <interceptor-ref name="defaultStack"/>   
  11.             <!-- 保存路径savePath依赖注入 -->         
  12.             <param name="savePath">/upload</param>
  13.             <result name="input"> /upload.jsp</result> 
  14.             <result>/succ.jsp</result>  
  15.         </action>
  16.     </package>
  17. </struts>

globalMessages.properties

  1. struts.messages.error.content.type.not.allowed=您上传的文件类型只能是图片文件!请重新选择!
  2. struts.messages.error.file.too.large=您要上传的文件太大,请重新选择!

UploadAction.java

  1. public class UploadAction extends ActionSupport {
  2.     private String title;
  3.     private File upload;
  4.     private String uploadContentType;
  5.     private String uploadFileName;
  6.     // 接受依赖注入的属性
  7.     private String savePath;
  8.     // 接受依赖注入的方法
  9.     public void setSavePath(String value) {
  10.         this.savePath = value;
  11.     }
  12.     private String getSavePath() throws Exception {
  13.         return ServletActionContext.getRequest().getRealPath(savePath);
  14.     }
  15.     public void setTitle(String title) {
  16.         this.title = title;
  17.     }
  18.     public void setUpload(File upload) {
  19.         this.upload = upload;
  20.     }
  21.     public void setUploadContentType(String uploadContentType) {
  22.         this.uploadContentType = uploadContentType;
  23.     }
  24.     public void setUploadFileName(String uploadFileName) {
  25.         this.uploadFileName = uploadFileName;
  26.     }
  27.     public String getTitle() {
  28.         return (this.title);
  29.     }
  30.     public File getUpload() {
  31.         return (this.upload);
  32.     }
  33.     public String getUploadContentType() {
  34.         return (this.uploadContentType);
  35.     }
  36.     public String getUploadFileName() {
  37.         return (this.uploadFileName);
  38.     }
  39.     @Override
  40.     public String execute() throws Exception {
  41.         // 以服务器的文件保存地址和原文件名建立上传文件输出流
  42.         FileOutputStream fos = new FileOutputStream(getSavePath() + "//"+ getUploadFileName());
  43.         FileInputStream fis = new FileInputStream(getUpload());
  44.         byte[] buffer = new byte[1024];
  45.         int len = 0;
  46.         while ((len = fis.read(buffer)) > 0) {
  47.             fos.write(buffer, 0, len);
  48.         }
  49.         return SUCCESS;
  50.     }
  51. }

抱歉!评论已关闭.