1、单文件上传
1.1 处理文件上传的Action类
public class UpLoadAction extends ActionSupport{
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
private FileInputStream fileInput;
private FileOutputStream fileOutput;
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath(){
String realpath = ServletActionContext.getServletContext().getRealPath(savePath);
return realpath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String execute() throws Exception{
upload.getAbsolutePath();
fileInput = new FileInputStream(getUpload());
fileOutput = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());
byte[] b = new byte[2048];
int len = 0;
while((len=fileInput.read(b))>0)
{
fileOutput.write(b,0,len);
}
fileInput.close();
fileOutput.close();
return "success";
}
}
1.2 JSP上传页面
<s:form action="upload" enctype="multipart/form-data">
选择文件:<s:file name="upload" ></s:file>
<s:submit value="上传"></s:submit>
</s:form>
//其他页面(略)
1.3 struts配置
//struts.xml
<struts>
<constant name="struts.devMode" value="false" />
<include file="struts-default.xml" />
<package name="up-load" extends="struts-default">
<action name="upload" class="com.action.UpLoadAction">
<param name="savePath">/uploadAdd</param>
<result name="success">upsuccess.jsp</result>
<result name="input">uperror.jsp</result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/gif,image/jpeg,image/x-png,image/bmp
</param>
<param name="maximumSize">2048000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
</action>
</package>
</struts>
2、多文件上传
2.1 处理文件上传的action类
public class UploadAction extends ActionSupport {
private List<File> files;
private List<String> filesFileName;
private List<String> filesContentType;
private String savedPath;
public List<File> getFiles() {
return files;
}
public void setFiles(List<File> files) {
this.files = files;
}
public List<String> getFilesFileName() {
return filesFileName;
}
public void setFilesFileName(List<String> filesFileName) {
this.filesFileName = filesFileName;
}
public List<String> getFilesContentType() {
return filesContentType;
}
public void setFilesContentType(List<String> filesContentType) {
this.filesContentType = filesContentType;
}
public String getSavedPath() {
String realPath=ServletActionContext.getServletContext().getRealPath(savedPath);
return realPath;
}
public void setSavedPath(String savedPath) {
this.savedPath = savedPath;
}
public String execute(){
try{
for(int i=0;i<files.size();i++){
OutputStream out=new FileOutputStream(new File(this.getSavedPath(),filesFileName.get(i)));
InputStream in=new FileInputStream(files.get(i));
byte [] buff=new byte[1024];
int len=0;
while(-1!=(len=in.read(buff))){
out.write(buff,0,len);
}
in.close();
out.close();
}
}
catch(Exception e){
e.printStackTrace();
}
return "success";
}
}
2.2 JSP上传页面
<h3>多文件上传</h3>
<s:form action="upload" enctype="multipart/form-data">
<s:file name="files"></s:file>
<s:file name="files"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
2.3 struts配置
//struts.xml
<struts>
<constant name="struts.devMode" value="false" />
<include file="struts-default.xml"/>
<package name="File" extends="struts-default">
<action name="upload" class="com.action.UploadAction">
<param name="savedPath">/uploadedfiles</param>
<result name="success">upload_success.jsp</result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/gif,image/jpeg,image/x-png,image/bmp</param>
<param name="maximumSize">10240000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
</action>
</package>
</struts>
2.4 上传成功页面
//显示上传的图片
<h3>上传成功</h3>
上传的文件:<br>
<s:iterator value="filesFileName" id="filename">
<s:property value="#filename"/>
<img src="uploadedfiles/<s:property value='#filename'/>" width="150" height="70"/>
<br>
</s:iterator>