今天老师让我们实现一个文件上传与下载的作业,上网查了好久,发现用struts2实现的文件上传下载比较简单,而且本人第一次写这种东西,故此留爪,以示本人经验值增加。。
文件的下载:
package com.ww.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport {
private String fileName;
private InputStream istream;
public InputStream getDownloadFile(){
istream = ServletActionContext.getServletContext().getResourceAsStream("/upload/"+fileName);
return istream;
}
@Override
public String execute() throws Exception {
if(!(istream == null)){
return SUCCESS;
}else{
return ERROR;
}
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public InputStream getIstream() {
return istream;
}
public void setIstream(InputStream istream) {
this.istream = istream;
}
}
前台的下载页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ 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>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File Download</title>
</head>
<body>
<s:a href="file/download?fileName=hero.xls">点击下载文件</s:a>
</body>
</html>
文件的上传:
package com.ww.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 UploadAction extends ActionSupport {
private File file;
private String fileFileName;
private String fileFileContentType;
@Override
public String execute() throws Exception {
InputStream is = new FileInputStream(file);
String path = ServletActionContext.getServletContext().getRealPath("/upload");
File file = new File(path);
if(!file.exists()){//不存在此文件夹就创建一个
file.mkdirs();
}
File destFile = new File(path,this.getFileFileName());
OutputStream os = new FileOutputStream(destFile);
byte[] size = new byte[400];
long num = 0;
while((num =is.read(size))>0){
os.write(size);
}
System.out.println(num);
is.close();
os.close();
return SUCCESS;
}
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 getFileFileContentType() {
return fileFileContentType;
}
public void setFileFileContentType(String fileFileContentType) {
this.fileFileContentType = fileFileContentType;
}
}
前台的上传页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ 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>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File Upload</title>
</head>
<body>
<s:form method="post" action="/file/upload" enctype="multipart/form-data"><!--enctype这项不能少,它表示你上传的是一个文件-->
<s:file name="file" ></s:file>
<s:submit value="上传"></s:submit>
</s:form>
</body>
</html>
struts.xml配置文件 :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="hero-default" namespace="/file" extends="struts-default">
<!-- 上载 -->
<action name="upload" class="com.ww.action.UploadAction">
<result>../uploadsuccess.jsp</result>
<result name="input">../upload.jsp</result>
<interceptor-ref name="fileUpload">
<param name="maximumSize">1024*1024*100*3</param>
<!-- <param name="allowedTypes">application/vnd.ms</param> -->
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<!-- 下载 -->
<action name="download" class="com.ww.action.DownloadAction">
<result type="stream">
<param name="contentDisposition">filename=${fileName}</param>
<param name="inputName">istream</param>
</result>
<result name="error">../downloadfail.jsp</result>
</action>
</package>
</struts>