<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/servlet/UploadServlet" enctype="multipart/form-data" method="post">
上传用户:<input type="text" name="username"><br/>
上传文件1:<input type="file" name="file1"><br/>
上传文件2:<input type="file" name="file2"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
//文件下载的后台servlet处理
package he.junhua.web.controller;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//1.得到解析器工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//2.得到解析器
ServletFileUpload upload = new ServletFileUpload(factory);
//3.判断上传表单的类型
if (!upload.isMultipartContent(request)) {
//上传表单为普通表单,则按照传统方式获取数据即可
return;
}
//为上传表单,则调用解析器解析上传数据
List<FileItem> list = upload.parseRequest(request);
for (FileItem item : list) {
//如果是普通输入项,则得到输入项的名称和值 username="xxx"
if(item.isFormField()){
String name = item.getFieldName();
String value = item.getString();
System.out.println(name+":"+value);
}else{
//得到上传文件的文件名
String filename = item.getName();
filename = filename.substring(filename.lastIndexOf("\\")+1);
//得到上传数据
InputStream in = item.getInputStream();
int len=0;
byte buffer[] = new byte[1024];
//设置保存路径,注意安全问题,即保存路径不能被外界直接访问到
String savepath = this.getServletContext().getRealPath("/WEB-INF/upload");
//向保存路径中写入数据
FileOutputStream out = new FileOutputStream(savepath+"\\"+filename);
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);
}
out.close();
in.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
/*
文件下载
文件下载时要注意文件名为中文时产生乱码的问题,在网上查询得如下解决方案:
先在页面做下判断,判断是什么浏览器,然后传一个参数到servlet,进行用不同的编码方式。
//判断当前使用浏览器版本类
var SysBrowser = function(){
this.ua = navigator.userAgent.toLowerCase();
this.browserAndVersion;
this.verIE ;
this.verFirefox;
this.verChrome;
this.verOpera;
this.verSafari;
this.getBrowser = function(){
(this.browserAndVersion = this.ua.match(/msie ([\d.]+)/)) ? this.verIE = parseInt(this.browserAndVersion[1],10) :
(this.browserAndVersion = this.ua.match(/firefox\/([\d.]+)/)) ? this.verFirefox = this.browserAndVersion[1] :
(this.browserAndVersion = this.ua.match(/chrome\/([\d.]+)/)) ? this.verChrome = this.browserAndVersion[1] :
(this.browserAndVersion = this.ua.match(/opera.([\d.]+)/)) ? this.verOpera = this.browserAndVersion[1] :
(this.browserAndVersion = this.ua.match(/version\/([\d.]+).*safari/)) ? this.verSafari = this.browserAndVersion[1] : 0;
//以下进行测试
if (this.verIE) return "IE"+this.verIE;
if (this.verFirefox) return "Firefox";
if (this.verChrome) return "Chrome";
if (this.verOpera) return "Opera";
if (this.verSafari) return "Safari";
}
};
// 设置Content-Disposition
String agent = request.getHeader("user-agent");
System.out.println(agent);
if (agent.contains("MSIE")) {
// IE浏览器 --- URL编码
filename = URLEncoder.encode(filename, "utf-8");
} else if (agent.contains("Firefox")) {
// 火狐浏览器 --- Base64编码
filename = base64EncodeFileName(filename);
} else {
filename = URLEncoder.encode(filename, "utf-8");
}
*/
package he.junhua.web.controller;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = this.getServletContext().getRealPath("/WEB-INF/upload/花菇.txt");
String filename = path.substring(path.lastIndexOf("\\")+1);
// response.setHeader("content-disposition", "attachment;filename="+filename);
response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename,"utf-8"));
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(path);
int len = 0;
byte buffer[] = new byte[1024];
out = response.getOutputStream();
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
相关文章
- [转]JSP或servlet中(以及上传下载文件)中文乱码或不显示的解决方案
- 跨浏览器PHP下载文件名中的中文乱码问题解决方法
- JavaWeb实现文件的上传与下载
- java中struts2实现简单的文件上传与下载
- 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理
- JavaWeb实现文件上传与下载的方法
- 在ASP.NET中,IE与Firefox下载文件带汉字名时乱码的解决方法
- javaEE项目Multipartfile实现文件上传下载并解决上传与下载文件中文乱码的问题
- javaEE项目Multipartfile实现文件上传下载并解决上传与下载文件中文乱码的问题
- php做下载文件的实现代码及文件名中乱码解决方法