Struts2中文件上传下载实例

时间:2023-03-08 19:13:32
Struts2中文件上传下载实例

1.单文件上传

 jsp页面:

 <!-- 单文件上传 -->
<form action="Fileupload.action" method="post"
enctype="multipart/form-data">
username:
<input type="text" name="username" />
<br />
file:
<input type="file" name="file" />
<br />
<input type="submit" name="提交" />
</form>
<!--注意:一定要写enctype="multipart/form-data"-->
 action页面:

 package com.Struts2.action;

 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 Fileupload extends ActionSupport { /**
*
*/
private static final long serialVersionUID = 4038542394937191173L; private String username;
private File file;
private String fileFileName;// 提交过来的file的名字,必须为FileName后缀
private String fileContentType;// 提交过来的file的MIME类型,必须以ContentType为后缀 public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public File getFile() {
return file;
} public void setFile(File file) {
this.file = file;
} public String getFileFileName() {
return fileFileName;
} public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
} public String getFileContentType() {
return fileContentType;
} public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
} @Override
public String execute() throws Exception {
String root = ServletActionContext.getServletContext().getRealPath(
"/upload");
InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File(root, fileFileName)); System.out.println("fileFileName:" + fileFileName);
// 因为file是存放在临时文件夹的文件,我们可以将其文件名和文件路径打印出来,看和之前的fileFileName是否相同
System.out.println("file:" + file.getName());
System.out.println("file:" + file.getPath()); byte[] buffer = new byte[500];// 用缓冲
int length = 0;
while (-1 != (length = is.read(buffer, 0, buffer.length))) {
os.write(buffer);
}
os.close();
is.close(); return SUCCESS;
}
}

2.多文件上传

 jsp页面:

 <script type="text/javascript">
$(function() {
$("#button").click(function (){
var html = $("<input type='file' name='file'/><br/>");
var button = $("<input type='button' name='button' value='删除'/><br/>");
$("#body div").append(html).append(button);
button.click(function (){
html.remove();
button.remove();
});
});
});
</script>
<body id="body">
<!-- 多文件上传 -->
<form action="manyFileupload.action" method="post"
enctype="multipart/form-data">
username:
<input type="text" name="username" />
<br />
file:
<input type="file" name="file" />
<br />
<input type="button" value="添加" id="button" />
<br />
<div></div>
<input type="submit" value="提交" />
</form>
</body>
action页面:

package com.Struts2.action;

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; /**
* 多文件上传
*
* @author admin
*
*/
public class ManyFileupload extends ActionSupport { /**
*
*/
private static final long serialVersionUID = -1060365794705605882L; private String username;
private List<File> file;// 因为有多个文件,所以用list集合,file属性指上传到的临时空间文件夹(upload)中的绝对路径
private List<String> fileFileName;// 上传的文件名称,自动会加载,因为是以FileName为后缀,会自动识别文件名称
private List<String> fileContentType;// 上传过来的文件类型,以ContentType后缀,自动识别类型(如:图片类型就是image/jepg) public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public List<File> getFile() {
return file;
} public void setFile(List<File> file) {
this.file = file;
} public List<String> getFileFileName() {
return fileFileName;
} public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
} public List<String> getFileContentType() {
return fileContentType;
} public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
} @Override
public String execute() throws Exception {
String root = ServletActionContext.getServletContext().getRealPath(
"/upload");
for (int i = 0; i < file.size(); i++) {
InputStream is = new FileInputStream(file.get(i));
OutputStream os = new FileOutputStream(new File(root, fileFileName
.get(i))); byte[] buffer = new byte[500];
int length = 0;
while (-1 != (length = is.read(buffer, 0, buffer.length))) {
os.write(buffer);
}
os.close();
is.close();
}
return super.execute();
} }

3.文件下载

 jsp页面

         <!-- 下载 -->
<form action="fileDownload.action">
<input type="submit" value="下载" />
</form>
action页面

package com.Struts2.action;

import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
* 文件下载
*
* @author admin
*
*/
public class FileDownload extends ActionSupport { /**
*
*/
private static final long serialVersionUID = 509550154000070698L; public InputStream getDownloadFile() {
return ServletActionContext.getServletContext().getResourceAsStream(
"upload/psb.jpg");// 从upload中选择一张图片进行下载
} @Override
public String execute() throws Exception {
return super.execute();
}
}

4.struts.xml

     <!-- 定义全局结果:必须写在action上面 -->
<global-results>
<result name="success">/success.jsp</result>
</global-results> <!-- 单文件上传 -->
<action name="Fileupload" class="com.Struts2.action.Fileupload"></action>
<!-- 多文件上传-->
<action name="manyFileupload" class="com.Struts2.action.ManyFileupload"></action>
<!-- 文件下载 -->
<action name="fileDownload" class="com.Struts2.action.FileDownload">
<result name="success" type="stream">
<param name="contentDisposition">attachment;filename="psb.jpg"</param><!-- attachment表示弹出下载提示框 -->
<param name="inputName">downloadFile</param><!-- downloadFile对应action中的getDownloadFile方法 -->
</result>
</action>

本博客源码出自:http://www.cnblogs.com/xiaoluo501395377/archive/2012/10/26/2740882.html。谢谢他的分享!