[color=darkred][b]1.文件上传[/b][/color]
[color=red] upload.jsp[/color]
[color=red] UploadAction.java[/color]
[color=red] struts.xml[/color]
[color=red] succ.jsp[/color]
[color=blue][b]
2.文件下载[/b][/color]
[color=red] download.jsp[/color]
[color=red] DownloadAction.java[/color]
[color=red] stuts.xml[/color]
[color=red] upload.jsp[/color]
<body>
<span style="color:red"><s:property value="#request.typeError"/></span>
<span style="color:red"><s:property value="#request.sizeError"/></span>
<span style="color:red"><s:fielderror/></span>
<form action="upload.action" method="post" enctype="multipart/form-data">
文件标题:<input type="text" name="title"/><br/>
选择文件:<input type="file" name="upload"/><br/>
<input type="submit" value="上传"/>
</form>
</body>
[color=red] UploadAction.java[/color]
package com.cs.struts2.helloworld;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
private String allowTypes;
private int fileSize;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
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() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
@Override
public String execute() throws Exception {
System.out.println(this.getUploadContentType());
/*
//验证类型
String filterResult = this.filterType(getAllowTypes().split(","));
if(filterResult != null) {
ActionContext.getContext().put("typeError", "上传的文件类型错误");
return filterResult;
}
//验证大小
String filterSizeResult = this.filterSize(this.getFileSize());
if(filterSizeResult != null) {
ActionContext.getContext().put("sizeError", "上传的文件大小错误");
return filterSizeResult;
}
*/
FileOutputStream fos = new FileOutputStream(this.getSavePath()
+"\\"+this.getUploadFileName());
FileInputStream fis = new FileInputStream(this.getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while((len=fis.read(buffer))>0) {
fos.write(buffer,0,len);
}
return SUCCESS;
}
/*
public String getAllowTypes() {
return allowTypes;
}
public void setAllowTypes(String allowTypes) {
this.allowTypes = allowTypes;
}
//过滤类型
public String filterType(String[] types) {
String fileType = this.getUploadContentType();
for(String type:types) {
if(fileType.equals(type)) {
return null;
}
}
return INPUT;
}
public int getFileSize() {
return fileSize;
}
public void setFileSize(int fileSize) {
this.fileSize = fileSize;
}
//过滤大小(1M=1024*1024)
public String filterSize(int fileSize) {
if(this.getUpload().length()<= fileSize) {
return null;
}
return INPUT;
}
*/
}
[color=red] struts.xml[/color]
<action name="upload" class="com.cs.struts2.helloworld.UploadAction">
<param name="savePath">/upload</param>
<!--
<param name="allowTypes">image/jpeg,image/bmp,image/gif,image/jpeg</param>
<param name="fileSize">71680</param>
-->
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/bmp,image/gif,image/jpeg</param>
<param name="maximumSize">71680</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<result>/succ.jsp</result>
<result name="input">upload.jsp</result>
</action>
[color=red] succ.jsp[/color]
<body>
上传成功!
文件标题:<s:property value="title"/><br/>
文件为: <IMG src="<s:property value="'upload/'+uploadFileName"/>"/><br/>
</body>
[color=blue][b]
2.文件下载[/b][/color]
[color=red] download.jsp[/color]
<body>
<a href="download.action">下载</a>
</body>
[color=red] DownloadAction.java[/color]
package com.cs.struts2.helloworld;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport {
private String inputPath;
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
public InputStream getTargetFile() throws Exception{
return ServletActionContext.getServletContext()
.getResourceAsStream(inputPath);
}
@Override
public String execute() throws Exception {
ActionContext act = ActionContext.getContext();
String user = (String)act.getSession().get("user");
if(user!=null&&user.equals("cs")) {
return SUCCESS;
}
act.put("tip", "您还没有登陆,请登录后再操作!");
return INPUT;
}
}
[color=red] stuts.xml[/color]
<action name="download" class="com.cs.struts2.helloworld.DownloadAction">
<param name="inputPath">/upload/Sunset.jpg</param>
<result name="success" type="stream">
<param name="contentType">image/jpg</param>
<param name="inputName">targetFile</param>
<param name="contentDisposition">filename="cc.jpg"</param>
<param name="bufferSize">102400</param>
</result>
<result name="input">login.jsp</result>
</action>