前提:
1.你得项目中已经导入了使用struts2上传的jar
2.jar包有两个分别是commons-fileupload-1.2.1.jar commons-io-1.3.2.jar 这两缺一不可
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>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="test/ActionTest!aiWei" method="post" enctype="multipart/form-data">
<input type="file" name="myFile" ><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
struts.xml配置: 其实就是基本配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.multipart.saveDir" value="d://"/>
<package name="test" namespace="/test" extends="struts-default">
<action name="ActionTest" class="com.silveraegis.upload.ActionTest">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
具体实现的action:主要内容是标红代码
package com.silveraegis.upload;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.servlet.ServletContext;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
public class ActionTest extends ActionSupport {
private File myFile; //必须和页面的上传元素name属性值一样
private String myFileFileName;// String属性名称蓝色部分必须和File属性名称一样 区分大小写 (将要上传的文件名称)
private String myFileContentType;//String属性名称蓝色部分必须和File属性名称一样 区分大小写 (将要上传的文件内型)
//必须给以上三个属性提供get() set()
public String aiWei() throws Exception{
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("C://"+this.getMyFileFileName());
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int len = 0;
while((len=bis.read(b))!=-1){
bos.write(b, 0, len);
}
bos.close();
fos.close();
bis.close();
fis.close();
return "success";
}
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;
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}
}