之前转载过一篇上传与下载的文章 ,参考一下自己整理验证了一下
package com.jereh.edu.entity;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
public class UpLoadAction {
private String uname ; //前台提交的用户名 ;
private File file; //上传文件
private String fileFileName; //上传文件 的文件名 前台自动提交不用另写 input
private String fileContentType ;/上传文件 的文件类型 前台自动提交不用另写 input
private InputStream inputStream ;//上传是用到的输入流 可以定义局部变量
private String fileName ;//前台隐藏上传的下载的文件名
private String downLoadPath; //下载路径
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) throws UnsupportedEncodingException{
byte[] b= fileName.getBytes("utf-8");
String str =new String(b,"iso-8859-1");
//获取时重新解码编码 ,解决乱码 和不显示中文的问题
this.fileName = str;
}
public String getDownLoadPath() {
return downLoadPath;
}
public void setDownLoadPath(String downLoadPath) {
this.downLoadPath = downLoadPath;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileContentName) {
this.fileFileName = fileContentName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String upFile() throws IOException{
//文件上传路径 ;
String path = ServletActionContext.getServletContext().getRealPath("/upload/");
// File upFile =new File(path+File.separatorChar+file.getName());
File upFile =new File(path+File.separatorChar+fileFileName);
upFile.getParentFile().mkdir();
//创建path路径
System.out.println("文件名+++++"+file.getName()+"---"+file.getPath());
//上传的file是个临时文件Struts2保存起来 我们把它导入到我们想要的路径下
System.out.println("上传的文件名+++++"+fileFileName+"---"+fileContentType);
inputStream =new FileInputStream(file) ;
//从临时文件里读取
OutputStream ops =new FileOutputStream(upFile);
//输出到指定文件
byte[] b =new byte[1024] ;
int length ;
while((length=inputStream.read(b,0,b.length))!=-1){
ops.write(b,0,length);
}
ops.close();
inputStream.close();
return Action.SUCCESS;
}
public InputStream getDownLoad() {
return ServletActionContext.getServletContext().getResourceAsStream(downLoadPath);
}
public String downLoadFile(){
return Action.SUCCESS ;
}
}
有配置过Struts的一看就会明白
<package name="upload" namespace="/upload" extends="struts-default" >
<action name="upFile" method="upFile" class="com.jereh.edu.entity.UpLoadAction">
<result name="success">
/success.jsp
</result>
</action>
<action name="downLoadFile" method="downLoadFile"
class="com.jereh.edu.entity.UpLoadAction">
<result name="success" type="stream">
<param name="contentDisposition">attachment;filename=${fileName}</param>
<param name="inputName">downLoad</param>
</result>
</action>
</package>
<param name="contentDisposition">attachment;filename=${fileName}</param>
contentDisposition 配置了前台是弹出下载框的形式下载 否则 直接显示在页面上 filename 是下载框提示的下载文件的名字
前台jsp form 表单
<pre name="code" class="html"><%@ 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>upFile</title>
</head>
<body>
<form action="upload/upFile.action" method ="post" enctype="multipart/form-data">
<p>
用户名:
</p>
<p><input type="text" name="uname"></p>
<p>
文件:
</p>
<p>
<input type="file" name="file">
</p>
<p>
<input type="submit" value="提交">
</p>
</form>
<br/>
<h3>下载</h3>
<form action="upload/downLoadFile.action" method="post">
<input type="hidden" name="downLoadPath" value="/upload/新建文本文档.txt">
<input type="hidden" name="fileName" value="新建文本文档.txt">
<input type="submit" value="下载">
</form>
</body>
</html>
上传的表单的
<pre name="code" class="html">enctype="multipart/form-data"
属性必须添加 而且method="post" 方式提交 为了数据的完整性jsp中隐藏了文件路径和文件名 做的项目了可以改成js代码 就不多说了