【SSH三大架構】Struts2基礎第五篇:檔案上傳

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   java   sp   for   檔案   

首先,我們建立一個web項目,然後匯入:commons-fileupload、commons-io這兩個jar包,當然還有其它的struts2需要的jar包

其次,我們還是先配置一下struts2的攔截器: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">  <display-name></display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

然後,我們寫一個前台介面,作為上傳檔案:file.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head><title>檔案上傳</title>  </head>    <body>    <form action="upload.action" enctype="multipart/form-data" method="post">    檔案:<input type="file" name="image">    <input type="submit" value="上傳">    </form>  </body></html>
可以看到,我們在上邊的form表單中定義的屬性:

action:這是要提交到的action目錄

enctype=“multipart/form-data":是設定表單的MIME編碼。預設情況,編碼格式是:application/x-www-form-urlencoded,不能夠用於檔案上傳;當設定為:multipart/form-data,才能夠完成的傳遞檔案資料,進行下面的操作。


接下來,我們需要在struts.xml中配置一個action:

<?xml version="1.0" encoding="GBK"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd"><!-- 指定Struts 2設定檔的根項目 --><struts><constant name="struts.action.extension" value="do,action"/><constant name="struts.multipart.maxSize" value="10701096" /><package name="employee" extends="struts-default"><action name="upload" class="cn.fileupload.FileUploadAction"><result name="success">/message.jsp</result></action></package></struts>
這裡的<constant name="struts.multipart.maxSize" value="10701096" />是設定檔案最大上傳的容量大小,這裡設定的是10M


我們還需要建立一個處理請求的邏輯類:

package cn.fileupload;import java.io.File;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileUploadAction extends ActionSupport{private File image;//獲得檔案的檔案名稱,格式為:nameFileName,這裡格式中的name為jsp頁面中的name屬性private String imageFileName;public String getImageFileName() {return imageFileName;}public void setImageFileName(String imageFileName) {this.imageFileName = imageFileName;}public File getImage() {return image;}public void setImage(File image) {this.image = image;}public String addUI(){return "success";}public String execute() throws Exception{//獲得要存放圖片的絕對路徑String realpath = ServletActionContext.getServletContext().getRealPath("/images");System.out.println(realpath);//在路徑下建立上傳的檔案File savefile = new File(new File(realpath),imageFileName);if(image!=null){if(!savefile.getParentFile().exists()){//如果路徑不存在,則建立一個savefile.getParentFile().mkdirs();//把上傳的檔案儲存在路徑中FileUtils.copyFile(image, savefile);ActionContext.getContext().put("message", "上傳成功");}}return "success";}}

如果檔案上傳成功,我們就可以在項目根目錄下的/image目錄下找到上傳的檔案




【SSH三大架構】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.