一.單文件上傳
添加jar包
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
在webroot創建一個文件如upload用于保存上傳的文件
1.upload.jsp
<body>
<s:form action="upload" enctype="multipart/form-data">
<s:file name="file" label="file"></s:file>
<s:submit label="upload"></s:submit>
</s:form>
</body>
2.UploadAction.java
package com.test.upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private File file;
private String fileFileName;
private String fileContentType;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
@Override
public String execute() throws Exception {
//1.構建一個輸入流
InputStream is=new FileInputStream(file);
//2.構建一個上傳文件路徑
String root=ServletActionContext.getRequest().getRealPath("/upload");
//3.獲得一個本地文件
File diskFile=new File(root,this.getFileFileName());
//4.構建輸出流
OutputStream os=new FileOutputStream(diskFile);
//5.能過字節寫入輸出流
byte[] buffer=new byte[400];
int length=0;
while((length=is.read(buffer))>0)
{
os.write(buffer,0,length);
}
is.close();
os.close();
return SUCCESS;
}
}
3.配置struts.xml
<constant name="struts.multipart.saveDir" value="c:\"></constant>
<package name="FileuploadTest" extends="struts-default">
<action name="upload" class="com.test.upload.UploadAction">
<result name="success">/uploadSuccess.jsp</result>
</action>
</package>
二.多文件上傳
1.upload.jsp頁面
<body>
<s:form action="upload" enctype="multipart/form-data">
<s:file name="file" label="file"></s:file>
<s:file name="file" label="file1"></s:file>
<s:file name="file" label="file2"></s:file>
<s:submit label="submit"></s:submit>
</s:form>
</body>
2.UploadAction.java
package com.test.upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private List<File> file;
private List<String> fileFileName;
private List<String> fileContentType;
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
@Override
public String execute() throws Exception {
for (int i = 0; i < file.size(); i++) {
// 1.構建一個輸入流
InputStream is = new FileInputStream(file.get(i));
// 2.構建一個上傳文件路徑
String root = ServletActionContext.getRequest().getRealPath(
"/upload");
// 3.獲得一個本地文件
File diskFile = new File(root, this.getFileFileName().get(i));
// 4.構建輸出流
OutputStream os = new FileOutputStream(diskFile);
// 5.能過字節寫入輸出流
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
is.close();
os.close();
}
return SUCCESS;
}
}
3.struts.xml
同上
posted on 2008-05-08 11:23
長春語林科技 閱讀(821)
評論(0) 編輯 收藏 所屬分類:
struts2