您的位置:首页 > 编程语言 > Java开发

使用Struts2简单的基本文件上传下载实现

2015-11-26 16:46 826 查看
1.首先创建一个web项目

2.使用myeclipse自带的功能配置成struts项目

代码开始

首先是下载页面代码

<%@ 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>My JSP 'download.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>
<a href="downloadAction?fileName=F:/TEMP/AJAX.txt">下载文件</a> <br>
</body>
</html>
上传页面代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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 'UpLoad.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>
<br>
<s:form action="uploadAcion" method="post" enctype="multipart/form-data">
<s:file name="file" label="请选择要上传的问题件"></s:file>
<s:submit></s:submit>
</s:form>
</body>
</html>


下载的Action

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport{
private String fileName;
//固定参数名,用于得到文件内容类型
private String contentType;
//输入流,输入到内存
private InputStream inputStream;
//将页面上要下载的文件类型通过后缀名转换为服务器能识别的文件类型
static Map<String,String> contentTypes = new HashMap<String,String>();

static{
contentTypes.put("doc", "application/msword");
contentTypes.put("ppt", "application/vnd.ms-powerpoint");
contentTypes.put("xsl", "application/vnd.ms-excel");
contentTypes.put("pdf", "application/pdf");
contentTypes.put("html", "text/html");
contentTypes.put("htm", "application/html");
contentTypes.put("gif","image/gif");
contentTypes.put("jpeg","image/jpeg");
contentTypes.put("mpeg","video/mpeg");
}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public String getContentType(String fileName) {
for(String ext:contentTypes.keySet()){
if(fileName.toLowerCase().endsWith(ext)){
return contentTypes.get(ext);
}
}
//代表二进制传输文件
return "application/octet-stream";
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

public InputStream getInputStream() {
return inputStream;
}

public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}

@Override
public String execute() {
File file = new File(fileName);
try{
inputStream = new FileInputStream(file);
}catch(Exception e){
e.printStackTrace();
}
fileName = file.getName();
contentType = this.getContentType(fileName);
return SUCCESS;
}

}


上传的action

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
//file控件的名字
private String file;
//文件类型,后缀是固定的
private String fileContentType;
//文件的名字
private String fileFileName;
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
@Override
public String execute(){
try {
InputStream is = new FileInputStream(file);
//一次传输的大小
byte[] b = new byte[1024];
int length = is.read(b);

//目标文件夹
String path ="F:/TEMP";
File f = new File(path);
//如果不存在就创建
if(!f.exists()){
f.mkdir();
}
//输出流,从内存中输出到内存外
OutputStream os = new FileOutputStream(new File(path+"/"+fileFileName));
while(length>0){
os.write(b);
length = is.read();
}
is.close();
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;

}
}


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>
<package name="pk" extends="struts-default" namespace="/">
<action name="uploadAcion" class="action.UploadAction">
<result>/upload.jsp</result>
<result name="input">/upload.jsp</result>
</action>

<action name="downloadAction" class="action.DownloadAction">
<result type="stream">
<param name="contentType">${contentType}</param>
<param name="contentDisposition">attachment;filename=${fileName }</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">1024</param>
</result>

</action>
</package>
</struts>


配置struts.properties 代码

struts.multipart.parser=jakarta
struts.multipart.saveDir=C\:/TEMP
struts.multipart.maxSize=102400
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts2