文件下载

原创 2015年07月07日 23:34:01


2014年9月8日 16:43:59




package cn.itcast.web.controller;
 
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//显示所有上传的文件,封装到域对象中,交给jsp去显示
public class ShowAllFilesServlet extends HttpServlet {
 
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Map<String, String> map = new HashMap<String, String>();//key:UUID文件名;value:老文件名
//得到存储文件的根目录
String storePath = getServletContext().getRealPath("/WEB-INF/files");
//递归遍历其中文件
File file = new File(storePath);
treeWalk(file,map);
//交给JSP去显示:如何封装数据.用Map封装。key:UUID文件名;value:老文件名
request.setAttribute("map", map);
request.getRequestDispatcher("/listFiles.jsp").forward(request, response);
}
//遍历/WEB-INF/files所有文件,把文件名放到map中
private void treeWalk(File file, Map<String, String> map) {
if(file.isFile()){
//是文件
String uuidName = file.getName();// UUID_a_a.txt//真实文件名
String oldName = uuidName.substring(uuidName.indexOf("_")+1);
map.put(uuidName, oldName);
}else{
//是一个目录
File[] fs = file.listFiles();
for(File f:fs){
treeWalk(f,map);
}
}
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
doGet(request, response);
}
 
}




package cn.itcast.web.controller;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
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 {
response.setContentType("text/html;charset=UTF-8");
OutputStream out = response.getOutputStream();
String filename = request.getParameter("filename");//get请求方式
filename = new String(filename.getBytes("ISO-8859-1"),"UTF-8");//中文编码
//截取老文件名
String oldFileName = filename.split("_")[1];
//得到存储路径
String storePath = getServletContext().getRealPath("/WEB-INF/files");
//得到文件的全部路径
String filePath = makeStorePath(storePath, filename)+"\\"+filename;
//判断文件是否存在
File file = new File(filePath);
if(!file.exists()){
out.write("对比起!你要下载的文件可能已经不存在了".getBytes("UTF-8"));
return;
}
InputStream in = new FileInputStream(file);
//通知客户端以下载的方式打开
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(oldFileName, "UTF-8"));
byte[] b = new byte[1024];
int len = -1;
while((len=in.read(b))!=-1){
out.write(b, 0, len);
}
in.close();
out.write("下载成功".getBytes("UTF-8"));
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
doGet(request, response);
}
private String makeStorePath(String storePath, String fileName) {
int hashCode = fileName.hashCode();
int dir1 = hashCode & 0xf;// 0000~1111:整数0~15共16个
int dir2 = (hashCode & 0xf0) >> 4;// 0000~1111:整数0~15共16个
 
String path = storePath + "\\" + dir1 + "\\" + dir2; // WEB-INF/files/1/12
File file = new File(path);
if (!file.exists())
file.mkdirs();
 
return path;
}
}





<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
 
</head>
<body>
<h1>本站有以下好图片</h1>
<c:forEach items="${map}" var="me">
<c:url value="/servlet/DownloadServlet" var="url">
<c:param name="filename" value="${me.key}"></c:param>
</c:url>
${me.value}  <a href="${url}">下载</a><br/>
</c:forEach>
</body>
</html>

2014年9月8日 16:43:59




package cn.itcast.web.controller;
 
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//显示所有上传的文件,封装到域对象中,交给jsp去显示
public class ShowAllFilesServlet extends HttpServlet {
 
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Map<String, String> map = new HashMap<String, String>();//key:UUID文件名;value:老文件名
//得到存储文件的根目录
String storePath = getServletContext().getRealPath("/WEB-INF/files");
//递归遍历其中文件
File file = new File(storePath);
treeWalk(file,map);
//交给JSP去显示:如何封装数据.用Map封装。key:UUID文件名;value:老文件名
request.setAttribute("map", map);
request.getRequestDispatcher("/listFiles.jsp").forward(request, response);
}
//遍历/WEB-INF/files所有文件,把文件名放到map中
private void treeWalk(File file, Map<String, String> map) {
if(file.isFile()){
//是文件
String uuidName = file.getName();// UUID_a_a.txt//真实文件名
String oldName = uuidName.substring(uuidName.indexOf("_")+1);
map.put(uuidName, oldName);
}else{
//是一个目录
File[] fs = file.listFiles();
for(File f:fs){
treeWalk(f,map);
}
}
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
doGet(request, response);
}
 
}




package cn.itcast.web.controller;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
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 {
response.setContentType("text/html;charset=UTF-8");
OutputStream out = response.getOutputStream();
String filename = request.getParameter("filename");//get请求方式
filename = new String(filename.getBytes("ISO-8859-1"),"UTF-8");//中文编码
//截取老文件名
String oldFileName = filename.split("_")[1];
//得到存储路径
String storePath = getServletContext().getRealPath("/WEB-INF/files");
//得到文件的全部路径
String filePath = makeStorePath(storePath, filename)+"\\"+filename;
//判断文件是否存在
File file = new File(filePath);
if(!file.exists()){
out.write("对比起!你要下载的文件可能已经不存在了".getBytes("UTF-8"));
return;
}
InputStream in = new FileInputStream(file);
//通知客户端以下载的方式打开
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(oldFileName, "UTF-8"));
byte[] b = new byte[1024];
int len = -1;
while((len=in.read(b))!=-1){
out.write(b, 0, len);
}
in.close();
out.write("下载成功".getBytes("UTF-8"));
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
doGet(request, response);
}
private String makeStorePath(String storePath, String fileName) {
int hashCode = fileName.hashCode();
int dir1 = hashCode & 0xf;// 0000~1111:整数0~15共16个
int dir2 = (hashCode & 0xf0) >> 4;// 0000~1111:整数0~15共16个
 
String path = storePath + "\\" + dir1 + "\\" + dir2; // WEB-INF/files/1/12
File file = new File(path);
if (!file.exists())
file.mkdirs();
 
return path;
}
}





<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
 
</head>
<body>
<h1>本站有以下好图片</h1>
<c:forEach items="${map}" var="me">
<c:url value="/servlet/DownloadServlet" var="url">
<c:param name="filename" value="${me.key}"></c:param>
</c:url>
${me.value}  <a href="${url}">下载</a><br/>
</c:forEach>
</body>
</html>

版权声明:本文为博主原创文章,未经博主允许不得转载。

CGLib.jar文件下载

  • 2017年12月09日 15:39
  • 280KB
  • 下载

文件下载神器 Curl For Windows x32 x64

  • 2017年11月17日 22:37
  • 5.25MB
  • 下载

Webpack 删除重复文件的一种优化思路

clean-webpack-plugin 删除重复文件的一种优化思路

解析excel表获取数据,并从远程服务器下载和上传文件

package com.primecredit.dayend; import java.io.BufferedInputStream; import java.io.BufferedOutputSt...

C#上传(修改表单数据),下载文件到服务器

由于项目的需要,需要将文件上传到服务器,并且冲服务器下载文件到客户端,采用的是HTTP协议,有人认为使用FTP协议更简单一些,并且效率更高一些。这里简单描述一些对比二者的一些特点:         (...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:文件下载
举报原因:
原因补充:

(最多只允许输入30个字)