struts2上傳圖片

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   使用   

在WEB-INF下建立一個content目錄,建立一個upload.jsp

 1 <%@ page contentType="text/html; charset=UTF-8" language="java" errorPage="" %> 2 <%@taglib prefix="s" uri="/struts-tags"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 4     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head> 7     <title>簡單的檔案上傳</title> 8 </head> 9 <body>10 <s:form action="uploadPro" method="post"11     enctype="multipart/form-data">12     <s:textfield name="title" label="檔案標題"/><br />13     <s:file name="upload" label="選擇檔案"/><br />14     <s:submit value="上傳"/>15 </s:form>16 </body>17 </html>

然後UploadAction.java如下

package action;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionSupport;import org.apache.struts2.ServletActionContext;import java.io.File;import java.io.*;public class UploadAction extends ActionSupport{private String title;private File upload;private String uploadContentType;private String uploadFileName;private String savePath;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public File getUpload() {return upload;}public void setUpload(File upload) {this.upload = upload;}public String getUploadContentType() {return uploadContentType;}public void setUploadContentType(String uploadContentType) {this.uploadContentType = uploadContentType;}public String getUploadFileName() {return uploadFileName;}public void setUploadFileName(String uploadFileName) {this.uploadFileName = uploadFileName;}public String getSavePath() {return ServletActionContext.getServletContext().getRealPath(savePath);}public void setSavePath(String savePath) {this.savePath = savePath;}@Overridepublic String execute() throws Exception{FileOutputStream fos = new FileOutputStream(getSavePath()+"//"+getUploadFileName());FileInputStream fis = new FileInputStream(getUpload());byte[] buffer = new byte[1024];int len = 0;while((len = fis.read(buffer)) > 0){fos.write(buffer, 0 , len);}fis.close();fos.close();return SUCCESS;}}

 在struts2中,如果表單中包含一個name屬性為xxx的檔案域,則對應action需要使用三個屬性來封裝該檔案域的資訊。

1.類型為File的xxx屬性封裝了該檔案域對應的檔案內容。

2.類型為String的xxxFileName屬性封裝了該檔案域對應的檔案的檔案名稱。

3.類型為String的xxxContentType屬性封裝了該檔案域對應的檔案的檔案類型。

上面例子struts2直接將檔案域包含的上傳檔案名稱和檔案類型的資訊封裝到uploadFileName和uploadContentType屬性中。

下面是sruts.xml配置

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts><constant name="struts.custom.i18n.resources" value="mess"/><!-- 設定該應用使用的解碼集 --><constant name="struts.i18n.encoding" value="GBK"/><package name="lee" extends="struts-default"><!-- 配置處理檔案上傳的Action --><action name="uploadPro" class="action.UploadAction"><!-- 動態設定Action的屬性值 --><param name="savePath">/upload</param><!-- 配置Struts 2預設的視圖頁面 --><result>/WEB-INF/content/succ.jsp</result></action><action name="*"><result>/WEB-INF/content/{1}.jsp</result></action></package></struts>

 然後是web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  <filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><!-- 讓Struts 2的核心Filter攔截所有請求 --><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

succ.jsp:

<%@ page contentType="text/html; charset=UTF-8" language="java" errorPage="" %><%@taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>上傳成功</title></head><body>上傳成功!<br/>檔案標題:<s:property value=" + title"/><br/>檔案為:<img src="<s:property value="‘upload/‘ + uploadFileName"/>"/><br/></body></html>

 

 

struts2上傳圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.