在配置文件struts.xml中:
<!-- 文件上传 -->
<action name="file" class="com.kaishengit.action.FileAction">
<result>/WEB-INF/views/upload.jsp</result>
</action>
<action name="upload" class="com.kaishengit.action.FileAction" method="upload">
<result type="redirectAction">
<param name="actionName">file</param>
</result>
</action>
<!-- 文件下载 -->
<action name="download" class="com.kaishengit.action.DownloadAction">
<result type="stream">
<param name="contentType">${type}</param>// 文件类型
<param name="contentLength">${length}</param>//文件大小
<param name="contentDisposition">attachment;filename="${fileName}"</param>//文件名
<param name="inputName">file</param>//在接受的action中必须要有对应的方法;
<param name="bufferSize">1024</param>//缓存区大小
<param name="allowCaching">true</param>// 是否需要缓存
<param name="contentCharSet">UTF-8</param>
</result>
</action>
在action中,这里我写的是多个文件上传的;
上传的FileAction
package com.kaishengit.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import com.opensymphony.xwork2.Action;
public class FileAction {
private String fileDesc;
private File[] documents;//这里的documents必须跟jsp中文件file类型的name属性一样;
private String[] documentsFileName;//文件名必须以FileName结尾;这是java中的约定;
private List<String> documentsContentType;
public String execute() {
return Action.SUCCESS;
}
public String upload() throws Exception {
for(int i = 0;i < documents.length;i++) {
File document = documents[i];
System.out.println("------------------------------------------");
System.out.println("FileName :" + documentsFileName[i]);
System.out.println("documentContentType:" + documentsContentType.get(i));
System.out.println(document.getName());
System.out.println(document.length());
System.out.println("------------------------------------------");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(document));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:/upload/"+documentsFileName[i]));
byte[] buffer = new byte[1024];
int len = -1;
while((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.flush();
bos.close();
bis.close();
}
return Action.SUCCESS;
}
public String getFileDesc() {
return fileDesc;
}
public void setFileDesc(String fileDesc) {
this.fileDesc = fileDesc;
}
public File[] getDocuments() {
return documents;
}
public void setDocuments(File[] documents) {
this.documents = documents;
}
public String[] getDocumentsFileName() {
return documentsFileName;
}
public void setDocumentsFileName(String[] documentsFileName) {
this.documentsFileName = documentsFileName;
}
public List<String> getDocumentsContentType() {
return documentsContentType;
}
public void setDocumentsContentType(List<String> documentsContentType) {
this.documentsContentType = documentsContentType;
}
}
下载的action:
package com.kaishengit.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import com.opensymphony.xwork2.Action;
public class DownloadAction {
private String type;
private long length;
private String fileName;
public String execute() {
return Action.SUCCESS;
}
public InputStream getFile() throws Exception {
type = "application/pdf";
fileName = new String("简历.pdf".getBytes("UTF-8"),"ISO8859-1");
File file = new File("C:/upload/123.pdf");
InputStream input = new FileInputStream(file);
length = file.length();
return input;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
上传和下载的jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'upload.jsp' starting page</title>
</head>
<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
文件的描述:<input type="text" name="fileDesc"/><br/>
<input type="file" name="documents"/><br/>
<input type="file" name="documents"/><br/>
<input type="submit" value="上传"/>
</form>
<a href="download.action">点击下载</a>
<br/>
</body>
</html>
分享到:
相关推荐
一个利用Struts2实现文件的上传下载,使用的是同步方式。没有用ajax请求
可以单文件或多文件上传和下载,如需要改变上传路径例如上传到本地磁盘(D:\)下只需稍加修改即可,再次非常感谢大虾李顺利。
注意:private String uploadContextType;应更正为private String uploadContentType;不过不影响正常运行
利用struts2实现文件上传,这里面有批量上传,和单一文件上传,功能实现很简单,但是必须先下载STRUTS2的jar包,以及在前台传入数据时候注意我的form表单,用sturts2上传必须改变form表单
用Struts1.2实现文件上传功能,并且把文件保存在javaweb project的虚拟路径下。利用fileForm 类型上传二进制文件格式和字符串格式数据。
超级简单. 一看就懂. 包您满意. 利用struts2和json实现上传文件实时监控. 只要将项目中struts2与json的jar包加上就可以使用
通过struts2框架实现Struts2文件上传下载以及显示。综合实现数据的利用。
利用Struts2实现的文件上传和下载,可以实现多文件上传,下载有管控。仅示例代码,高手勿看!
利用struts实现文件的上传和下载功能示例
项目:Struts2UpAndDownLoadFile 1.实现文件的上传和下载 项目:Struts2HandleExcelFile 1.实现文件的上传和下载 2.利用apache的poi-3.5-FINAL-20090928.jar组件实现创建Excel表格。
struts文件的上传下载功能实现,并利用过滤器解决了相应的中文乱码问题
利用struts2框架实现uploadidfy多文件上传,显示进度条上传
利用struts组件完成上传到服务器。自由设定目录和文件类型,稍加修改添加到项目里面.把"备份上传Ex7_7.jsp"替换掉“Ex7_7.jsp”。前提是事先在tomcat目录建好upload文件夹
实现文件的上传和下载的源代码 决定没问题的。 利用了struts2 tomcat
利用struts2上传文件 环境配置JDK1.8 Eclipse 版本 Version: Mars Release (4.5.0) tomcat版本7.0.40 注意的问题是文件上传的时候的enctype="multipart/form-data" 如果设置错误会报错 今天初次使用Struts 2做文件...
4. 批量导入数据采用的是导入test文件夹下的测试压缩包upload.rar上传到服务器的临时目录,然后利用WinRar.exe对其进行解压,解压后对指定文件名的Excel文件进行解析,然后对每条记录所配置的文件路径进行比对然后...
struts 上传文件的示例,是一个myeclipse工程
实现struts1文件上传,利用struts1相关组件来实现的
文件上传一直是Ext3.0以上版本的难点,本实例利用Ext3.2和struts实现文件和参数的共同上传,并解决了中文乱码的问题。
利用struts2实现了上传功能和多文件上传功能,同时也实现了下载功能