Struts2实现文件的上传和下载
来源:互联网 发布:算法竞赛入门书籍推荐 编辑:程序博客网 时间:2024/05/16 13:42
web.xml源码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Loadfile</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
struts.xml源码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<!-- 指定每次请求到达,重新加载资源文件 -->
<constant name="struts.i18n.reload" value="true" />
<!-- 指定每次配置文件更改后,自动重新加载 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 把主题配置为simple -->
<constant name="struts.ui.theme" value="simple" />
<!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) -->
<constant name="struts.multipart.maxSize" value="10701096" />
<!--文件上传-->
<package name="upload" namespace="/" extends="struts-default">
<action name="Upload" class="action.FileUploadAction">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
<!--文件下载-->
<action name="FileDownload" class="action.FileDownload">
<result name="success" type="stream">
<param name="contentType">text/plain</param>
<param name="contentDisposition">attachment;fileName="${fileName}"</param>
<param name="inputName">downloadFile</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
FileDownload.java源码:
package action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
//文件下载
public class FileDownload extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
//返回一个输入流,作为一个客户端来说是一个输入流,但对于服务器端是一个 输出流
public InputStream getDownloadFile() throws Exception
{
this.fileName = "Dream.jpg" ;
//获取资源路径
return ServletActionContext.getServletContext().getResourceAsStream("upload/"+this.fileName) ;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
FileUploadAction.java源码:
package action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private File myFile;
private String myFileContentType;
private String myFileFileName;
private String destPath;
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
destPath = "e:/upload/"; //目标位置
try{
System.out.println("Src File name: " + myFile);
System.out.println("我的文件名"+myFileFileName);
System.out.println("我的文件类型"+ myFileContentType);
File destFile = new File(destPath, myFileFileName);
FileUtils.copyFile(myFile, destFile);
}catch(IOException e){
e.printStackTrace();
return ERROR;
}
return SUCCESS;
}
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
public String getDestPath() {
return destPath;
}
public void setDestPath(String destPath) {
this.destPath = destPath;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
index.jsp源码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="Upload.action" method="post" enctype="multipart/form-data">
<label for="myFile">你要上传的文件</label>
<input type="file" name="myFile" /><br>
<input type="submit" value="上传"/>
</form>
</body>
</html>
filedownload.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>文件下载</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h2>文件下载内容:</h2><br/>
Dream.jpg:<a href="FileDownload.action">点击下载</a><br/>
</body>
</html>
success.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>文件上传</title>
</head>
<body>
成功
</body>
</html>
- struts2 实现文件的上传 和 下载
- Struts2实现文件的上传和下载
- Struts2实现文件的上传和下载
- struts2实现文件的上传(仅限单文件)和下载
- struts2实现文件的上传和下载实例
- 使用Struts2实现简单的文件上传和下载
- 使用Struts2实现文件的上传和下载
- Struts2实现文件上传和下载
- struts2实现文件上传和下载
- struts2 实现文件上传和下载
- Struts2实现文件上传和下载
- Struts2实现文件上传和下载
- Struts2实现文件上传和下载
- Struts2实现文件上传和下载
- struts2完美实现文件上传和下载
- Struts2实现文件上传和下载
- Struts2实现文件上传和下载
- Struts2实现文件上传和下载
- Do We Really Need to Collect Millions of Faces for effective face recognition
- 控件的字体设置的两种方式
- 利用反射进行动态加载dll
- 压力测试工具JMeter入门教程
- 块级元素与行内元素的区别
- Struts2实现文件的上传和下载
- 【使用JMeter 对Web服务器压力测试】
- 一个Spring启动时找不到类的问题修复记录
- form标签的位置影响jQuery WeUI滚动加载使用的例子
- HttpURLConnection请求网络
- seetaface FaceDetection模块
- 说说那些电脑外设,数位板的保养与维修
- apache common Lang包StringUtils系列(四)
- 调通了ffmpeg tutorial例程