课程/Java/后端开发
Java中的文件上传下载
-
-
690017359
2017-11-26
- smartupload文件下载
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;filename=test.zip");
String path = "E:/Javajava/workspace for j2ee/Study2/WebContent/images";
String[] filenames = request.getParameterValues("filename");
String str = "";
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
for (String filename : filenames) {
File file = new File(path, filename);
str += filename + " ";
zos.putNextEntry(new ZipEntry(filename));
FileInputStream fis = new FileInputStream(file);
byte b[] = new byte[1024];
int n = 0;
while ((n = fis.read(b)) != -1) {
zos.write(b, 0, n);
}
zos.flush();
fis.close();
}
zos.setComment("download success" + " " + str);
zos.flush();
zos.close();
-
0赞 · 0采集
-
-
Zhq9695
2017-10-09
- smartupload文件下载
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;filename=test.zip");
String path = "E:/Javajava/workspace for j2ee/Study2/WebContent/images";
String[] filenames = request.getParameterValues("filename");
String str = "";
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
for (String filename : filenames) {
File file = new File(path, filename);
str += filename + " ";
zos.putNextEntry(new ZipEntry(filename));
FileInputStream fis = new FileInputStream(file);
byte b[] = new byte[1024];
int n = 0;
while ((n = fis.read(b)) != -1) {
zos.write(b, 0, n);
}
zos.flush();
fis.close();
}
zos.setComment("download success" + " " + str);
zos.flush();
zos.close();
-
0赞 · 0采集
-
-
DR枫林残忆
2017-06-02
- 批量下载
-
截图
0赞 · 0采集
-
-
DR枫林残忆
2017-06-02
- 批量下载
-
截图
0赞 · 0采集
-
-
DR枫林残忆
2017-06-02
- 批量下载
-
截图
0赞 · 0采集
-
-
无心水2012
2016-12-07
- 批量下载-ZipOutputStream
-
截图
0赞 · 0采集
-
-
BeingTowards
2016-09-16
- SmartUpload的批量下载:将多个文件打包成一个压缩包文件下载
setComment为压缩包里右边的注释说明
-
截图
0赞 · 1采集
-
-
为为_0002
2016-09-12
- smartupload实现文件批量下载2
-
截图
0赞 · 0采集
-
-
为为_0002
2016-09-12
- smartupload实现文件批量下载
-
截图
0赞 · 0采集
-
-
梦编猿
2016-07-28
- Java中的文件上传下载——BatchDownloadServlet.java
public class BatchDownloadServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("application/x-msdownload");
resp.setHeader("Content-Disposition", "attachment;filename=test.zip");
String path = getServletContext().getRealPath("/") + "images/";
String[] filenames = req.getParameterValues("filename");
String str = "";
String rt = "\r\n";
ZipOutputStream zos = new ZipOutputStream(resp.getOutputStream());
for(String filename : filenames){
str += filename + rt;
File file = new File(path + filename);
zos.putNextEntry(new ZipEntry(filename));
FileInputStream fis = new FileInputStream(file);
byte b[] = new byte[1024];
int n = 0;
while((n = fis.read(b)) != -1){
zos.write(b, 0, n);
}
zos.flush();
fis.close();
}
zos.setComment("download success:" + rt + str);
zos.flush();
zos.close();
}
}
-
截图
1赞 · 2采集
-
-
梦编猿
2016-07-28
- Java中的文件上传下载——SmartDownloadServlet.java
public class SmartDownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String filename = request.getParameter("filename");
SmartUpload su = new SmartUpload();
su.initialize(getServletConfig(), request, response);
su.setContentDisposition(null);
try {
su.downloadFile("/images/"+ filename);
} catch (SmartUploadException e) {
e.printStackTrace();
}
}
}
【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】
-
1赞 · 1采集
-
-
贫僧也是老衲
2016-07-28
- 设置文件下载
-
截图
0赞 · 0采集
-
-
zhuxc
2016-07-07
- 文件批量下载
-
截图
0赞 · 0采集
-
-
qq_颖_男人_03406653
2016-06-07
- zos.setCmmont()在压缩文件中打出下载文件信息
-
截图
0赞 · 0采集
-
-
qq_颖_男人_03406653
2016-06-07
- zos.putNextEntry(new ZipEntry(filename))设置文建条目
-
截图
0赞 · 0采集
-
-
庄学爸
2016-04-02
- //通过循环,把文件写入到ZipOutputStream当中.
for(String fileName:fileNames){
str+=fileName+rt;
File file=new File(path+fileName);
//通过ZipOutputStream的putNextEntry(ZipEntry e)方法设置需要压缩的文件条目.参数是ZipEntry对象,此类用于表示ZIP文件条目,条目:指按内容分列的细目.
//文件条目即把文件加入到Zip压缩包里.
ZipEntry类的构造方法参数是String类型.
zipo.putNextEntry(new ZipEntry(fileName));
//创建输入流,从服务器端读取客户端需要下载的文件的内容,同时将内容通过ZipOutputStream输出流写入.
FileInputStream inputStream=new FileInputStream(file);
byte [] b=new byte[1024];
int n=0;
while((n=inputStream.read(b))!=-1){
zipo.write(b, 0, n);
}
//刷新此输出流,关闭输入流
//zipo.flush();
inputStream.close();
}
//ZipOutputStream的setComment(String comment)方法可以设置ZIP文件注释,下载打开压缩包时可以从右边看到该内容.
zipo.setComment("download:success"+rt+str);
//刷新输出流,不刷新结果也没什么影响.
//zipo.flush();
//关闭输出流
zipo.close();
}
-
0赞 · 0采集
-
-
庄学爸
2016-04-02
- Servlet中:
doPost(){
//首先设置contentType类型为下载类型
response.setContentType("application/x-msdownload");
//设置header信息,那么下载下来的zip压缩包名就为text.zip
response.setHeader("Content-Disposition","attachment;filename=test.zip");
//获取服务器中保存需要下载文件的目录.
String path=this.getServletContext().getRealPath("/")+"file/";
//获取表单提交需要下载的文件名数组(复选框).
String [] fileNames=request.getParameterValues("filenames");
//创建ZipOutputStream对象,此类为以ZIP文件格式写入文件实现输出流过滤器.构造方法参数是OutPutStream对象,因此可以使用response.getOutPutStream作为参数.
ZipOutputStream zipo=new ZipOutputStream(response.getOutputStream());
//该字符串用来设置Zip文件注释
String str="";
//定义换行符
String rt="\r\n";
-
0赞 · 0采集
-
-
庄学爸
2016-04-02
- 在实际项目当中,想要实现文件批量下载,基本上都是通过打包的方式.
即假如下载10个文件,此时把这10个文件打包在一个zip的压缩包里.
smartupload实现批量下载:
jsp前台页面:
<h2>批量下载:</h2>
<!-- 表单不需要设置enctype属性,因为不是上传文件 -->
<form action="batchDownLoad" method="post" >
<input type="checkbox" name="filenames" value="a.txt"/>a.txt
<input type="checkbox" name="filenames" value="b.txt"/>b.txt
<input type="checkbox" name="filenames" value="c.txt"/>c.txt
<input type="submit" value="确认"/>
</form>
-
0赞 · 0采集
-
-
庄学爸
2016-04-02
- request.getInputStream()与response.getOutputStream()方法是为了在无法指定file对象时,可以通过此方法创建到输入/输出流.
-
0赞 · 0采集
-
-
庄学爸
2016-04-02
- getParameter()方法就是根据表单中的name值,获取该name值对应表单项的value属性值.
getParameterValues();方法就是根据表单中的name值,获取该name值对应一组表单项的value属性值(用在复选框).
-
0赞 · 0采集
-
-
ITLover
2016-03-17
- 下载时response头信息设置
-
截图
0赞 · 0采集
-
-
Godtrue
2016-03-05
- 文件批量下载的原理:将多个文件压缩成一个zip包下载。
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("application/x-msdownload");
resp.setHeader("Content-Disposition", "attachment;filename=test.zip");
String path = getServletContext().getRealPath("/") + "images/";
String[] filenames = req.getParameterValues("filename");
String str = "";
String rt = "\r\n";
ZipOutputStream zos = new ZipOutputStream(resp.getOutputStream());
for(String filename : filenames){
str += filename + rt;
File file = new File(path + filename);
zos.putNextEntry(new ZipEntry(filename));
FileInputStream fis = new FileInputStream(file);
byte b[] = new byte[1024];
int n = 0;
while((n = fis.read(b)) != -1){
zos.write(b, 0, n);
}
zos.flush();
fis.close();
}
zos.setComment("download success:" + rt + str);
zos.flush();
zos.close();
}
-
截图
0赞 · 0采集