package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class UploadAction{
private String title;//用来接收标题信息
private File myfile;//封装上传的文件
private String myfileContentType;//封装上传文件的内容类型
private String myfileFileName;//上传文件的文件名称
private String savePath;//保存路径
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getMyfile() {
return myfile;
}
public void setMyfile(File myfile) {
this.myfile = myfile;
}
public String getMyfileContentType() {
return myfileContentType;
}
public void setMyfileContentType(String myfileContentType) {
this.myfileContentType = myfileContentType;
}
public String getMyfileFileName() {
return myfileFileName;
}
public void setMyfileFileName(String myfileFileName) {
this.myfileFileName = myfileFileName;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
private void printInfo()
{
System.out.println("title:"+this.getTitle());
System.out.println("filename:"+this.getMyfileFileName());
System.out.println("文本类型:"+this.getMyfileContentType());
}
public String execute() throws Exception
{
System.out.println("----");
this.printInfo();
try {
InputStream is=new FileInputStream(this.getMyfile());
OutputStream os=new FileOutputStream(new File("e:\\test\\"+this.getMyfileFileName()));
byte[] buff=new byte[4196];
int len=0;
while((len=is.read(buff, 0, 4196))!=-1)
{
os.write(buff, 0, len);
}
is.close();
os.close();
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
return "success";
}
}
<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="starts.devMode" value="true"></constant>
<constant name="starts.i18n.encoding" value="utf-8"></constant>
<constant name="struts.multipart.maxSize" value="10737418240"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="Upload" class="test.UploadAction">
<result type="redirect">/ok.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class UploadAction{
private String title;//用来接收标题信息
private File myfile;//封装上传的文件
private String myfileContentType;//封装上传文件的内容类型
private String myfileFileName;//上传文件的文件名称
private String savePath;//保存路径
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getMyfile() {
return myfile;
}
public void setMyfile(File myfile) {
this.myfile = myfile;
}
public String getMyfileContentType() {
return myfileContentType;
}
public void setMyfileContentType(String myfileContentType) {
this.myfileContentType = myfileContentType;
}
public String getMyfileFileName() {
return myfileFileName;
}
public void setMyfileFileName(String myfileFileName) {
this.myfileFileName = myfileFileName;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
private void printInfo()
{
System.out.println("title:"+this.getTitle());
System.out.println("filename:"+this.getMyfileFileName());
System.out.println("文本类型:"+this.getMyfileContentType());
}
public String execute() throws Exception
{
System.out.println("----");
this.printInfo();
try {
InputStream is=new FileInputStream(this.getMyfile());
OutputStream os=new FileOutputStream(new File("e:\\test\\"+this.getMyfileFileName()));
byte[] buff=new byte[4196];
int len=0;
while((len=is.read(buff, 0, 4196))!=-1)
{
os.write(buff, 0, len);
}
is.close();
os.close();
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
return "success";
}
}
<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="starts.devMode" value="true"></constant>
<constant name="starts.i18n.encoding" value="utf-8"></constant>
<constant name="struts.multipart.maxSize" value="10737418240"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="Upload" class="test.UploadAction">
<result type="redirect">/ok.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>