標籤:ipa patch lis prepare instance sof string web etc
須要的包檔案:
commons-fileupload-1.2.1.jarcommons-io-1.3.2.jarcommons-logging-1.1.jarfreemarker-2.3.13.jarognl-2.6.11.jarstruts2-core-2.1.6.jarxwork-2.1.2.jar
Struts2Test.java源碼:
package com.test;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")public class Struts2Test extends ActionSupport{private String picFileName;private File pic;public File getPic() {return pic;}public void setPic(File pic) {this.pic = pic;}public String getPicFileName() {return picFileName;}public void setPicFileName(String picFileName) {this.picFileName = picFileName;}public String upload() throws IOException {//輸出的檔案路徑以及檔案名稱java.io.File.File(String parent, String child)File upPic=new File(ServletActionContext.getServletContext().getRealPath("upload"),picFileName);FileInputStream in=null;FileOutputStream out=null;//得到父類路徑,假設不存在則建立upPic.getParentFile().mkdirs();in=new FileInputStream(pic); //讀入檔案out=new FileOutputStream(upPic); //輸出檔案int len=0; //資料長度byte[] byt=new byte[1024]; //每次讀入的資料包大小while((len=in.read(byt))!=-1){ //假設有資料讀入則輸出out.write(byt, 0, len);}in.close(); //關閉讀入流out.close(); //關閉輸出資料流return SUCCESS;}}
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><constant name="struts.devMode" value="true" /><package name="default" extends="struts-default" namespace="/"><action name="hello" class="com.test.Struts2Test" ><result name="success">/success.jsp</result></action></package> </struts>
web.xml源碼:
<?xml version="1.0" encoding="UTF-8"?><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"> <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>
index.jsp源碼:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@taglib prefix="s" uri="/struts-tags"%><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> </head> <body> <s:form action="hello!upload" enctype="multipart/form-data" method="post"> <!-- enctype="multipart/form-data" 此處是一個非常easy忽略的盲點 --> <s:file name="pic" label="上傳" /> <s:submit value="提交"/> </s:form> </body></html>
success.jsp源碼:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><html> <head> <base href="<%=basePath%>"> <title>SUCCESS</title> </head> <body> SUCCESS! <br> </body></html>
Struts2檔案的上傳