標籤:
1. upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib uri="/struts-tags" prefix="s"%><%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>uploadDemo</title> </head> <body> <s:form action="myUpload" enctype="multipart/form-data"> <s:file name="myfile"></s:file> <s:submit value="submit"></s:submit> </s:form> </body></html>
2 uploadResult.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML ><html> <head> </head> <body> Upload success!! </body></html>
3 UploadAction.java
package com.xiping.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport { private File myfile; // 對應真正上傳的檔案 private String myfileFileName; // 對應上傳的檔案名稱 public File getMyfile() { return myfile; } public void setMyfile(File myfile) { this.myfile = myfile; } public String getMyfileFileName() { return myfileFileName; } public void setMyfileFileName(String myfileFileName) { this.myfileFileName = myfileFileName; } @Override public String execute() throws Exception { InputStream is = new FileInputStream(myfile); OutputStream os = new FileOutputStream("E://temp3//" + myfileFileName); byte[] buffer = new byte[1024]; int length = 0; while (-1 != (length = is.read(buffer))) { os.write(buffer, 0, length); } os.close(); is.close(); return SUCCESS; }}
4 struts.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <!-- 所有匹配*.action的請求都由struts2處理 --> <constant name="struts.action.extension" value="action" /> <!-- 是否啟用開發模式 --> <constant name="struts.devMode" value="true" /> <!-- struts設定檔改動後,是否重新載入 --> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 設定瀏覽器是否緩衝靜態內容 --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 請求參數的編碼方式 --> <constant name="struts.i18n.encoding" value="utf-8" /> <!-- 每次HTTP請求系統都重新載入資源檔,有助於開發 --> <constant name="struts.i18n.reload" value="true" /> <!-- 檔案上傳最大值 --> <constant name="struts.multipart.maxSize" value="104857600" /> <!-- 讓struts2支援動態方法引動過程 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- Action名稱中是否還是用斜線 --> <constant name="struts.enable.SlashesInActionNames" value="false" /> <!-- 允許標籤中使用運算式文法 --> <constant name="struts.tag.altSyntax" value="true" /> <package name="struts" extends="struts-default"> <action name="myUpload" class="com.xiping.action.UploadAction"> <result name="success">/uploadResult.jsp</result> </action> </package></struts>
Struts學習總結-04 上傳檔案