Servlet 实现文件下载

2013-04-09  来源:本站原创  分类:Java  人气:0 

记录一下。

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {
String path = request.getParameter("path");//path为服务器上文件的绝对路径
File file = new File(path);
if(file.exists()){
String fileName = file.getName();
System.out.println("下载文件名为:"+fileName);
InputStream is = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[is.available()];
is.read(buffer);
is.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("GBK"),"ISO-8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");//设置文件传输格式,二进制流
os.write(buffer);
os.flush();
os.close();
}else{
System.out.println("文件不存在!");
}
}

相关文章
  • servlet实现文件下载的实用类分享 2014-12-05

    servlet文件下载类,为了防止客户端浏览器直接打开目标文件,在响应头里加入强制下载的MIME类型,大家参考使用吧 package com; import java.io.IOException; import java.io.PrintWriter; import java.net.URLEncoder; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpSe

  • Servlet 输出文件下载 2013-03-26

    1.如果下载的文件名是中文: 将文件名重新用"ISO8859_1"编码 2.下载自定义文件名: Response 设置头文件类型 "Content-disposition",值为" attachment;filename="文件名" " 3.获取文件的MIME类型 通过 ServletContext 的 getMimeType(String filename),即通过文件的后缀名得到注册在tomcat中的相应MIME类型 4.

  • Servlet 实现文件下载 2013-04-09

    记录一下. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getParameter("path");//path为服务器上文件的绝对路径 File file = new File(path); if(file.exists()){ String file

  • [转]Liferay使用心得 2012-03-28

    Liferay是一个出色的Java开源Portal产品,其中整合了很多当今流行的开源框架,也被不少人使用在实际项目中.最近刚结束了一个基于Liferay Portal的项目,总结下使用中的一些心得,希望对正在使用Liferay的朋友有些帮助. 一.安装 Liferay的安装十分容易,甚至都不能称其为安装,根据情况从Liferay官方网站下载合适的安装包(一般可以选择Tomcat的Bundle),解压后直接运行,它会使用自带的HSQL数据库,其中包括一些初始数据和演示数据. 实际使用时不能用HSQ

  • JavaEE实现文件下载 2014-05-07

    这篇文章主要介绍了JavaEE实现文件下载的方法,非常的实用,需要的朋友可以参考下 我们先来看一个最简单的文件下载的例子: package com.yyz.response; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServ

  • javaweb开发之处理表单上传文件和文件下载 2013-12-06

    一.基于表单的上传文件 1. enctype属性 当表单需要上传文件时,需指定表单 enctype 的值为 multipart/form-data. 在 form 元素的语法中,enctype 属性指定将数据发送到服务器时浏览器使用的编码类型. enctype 属性取值: application/x-www-form-urlencoded:表单 enctype 属性的默认值.这种编码方案使用有限的字符集,当使用了非字母和数字时,必须用"%HH"代替(H 代表十六进制数字).对于大容量的

  • PrimeFaces 4.0 使用 p:fileDownload 标签实现文件下载 2013-12-18

    这两天在使用 primefaces 4.0 做壹個下载文件的功能,去官方网站 primefaces.org 看了下,只有壹個 PDF 版用户指南和各种用例的在线 Demo ,目前可以在官方网站上找到的下载案例见于 http://www.primefaces.org/showcase/ui/fileDownload.jsf,能够看到部分源代码,但是找不到整個工程,实在是很不方便.后来又通过 Google 搜索了下,终于让我在 http://networkedblogs.com/B8rUw 找到了

  • 关于SpringMVC中文件下载小记 2014-02-13

    正在学习SpringMVC的路上,今天项目中需要用到文件下载的一个小功能,于是乎开始各种找资料看SpringMVC中是如何通过MessageConverter来实现的,其实做完发现还是比较简洁的,过程遇到几个问题: 下载下来的文件内容异常 IE下无法下载 IE下下载时文件名乱码 上代码 /** * 文件下载 * * @param fname 文件名称(含后缀) * @throws IOException */ @RequestMapping("/down") public Respon

  • Servlet brief introduction 2009-02-27

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s

  • In the servlet use Bean 2009-02-27

    According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, through the package into a property and methods of treatment of a function or a business object, referred to as bean. Because JavaBean is bas

  • WebQQ, ExtJs + Servlet + Hibernate + Spring implementation 2009-02-27

    Code for the development of boredom when using ExtJs + Servlet + hibernate (Ant + xdoclet generate HBM files) + spring implementation, Pure whim, but implementation has been more than chat, group chat, what's not achieve, nor how to consider the perf

  • NoClassDefFoundError: javax / servlet / Servlet 2009-02-27

    In the project in order to achieve a listener in web.xml set up a listener, did start in Tomcat actually occurred after java.lang.NoClassDefFoundError: javax / servlet / ServletContextListener this anomaly google and found the reasons for the lack of

  • servlet specification 2.2 2009-02-28

    Introduction: Spring Security watch, when to see more use of the Servlet Listener, filter and so on. Filter done, listener never. Therefore want from the Internet to find a closer look at the servlet specification, go thorough investigation, there is

  • Pring Beanfactory at the jsp, servlet, web.xml and other configuration 2009-03-03

    Keywords: spring beanfactory Spring in the web application called the Beanfactory 1) Configure web.xml Java code <? xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE web-app PUBLIC "- / / Sun Microsystems, Inc. / / DTD

  • prototype Ajax + Servlet (attached Demo) 2009-03-05

    Test WEB project set up prototype.js location WebContent / js / prototype.js Create test2.jsp location WebContent/test/test2.jsp <%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!DO

  • The second stage of examination (HTML.Servlet.JSP.Javascript.Ajax.JQuery) 2009-03-09

    ------------------------------------- Following is the pen questions (questions 2 points each) --- --------------------------------------------- 1. The use of HTTP protocol client request sent to the server-side which has two ways? Please describe br

  • Servlet 2.5 new features 2009-03-29

    September 26, 2005, Sun Company and the JSR154 Expert Group on Servlet API released a new version. Under normal circumstances, a new version of JSR including only a small number of previous removal of phantom norms update. But this time, the new vers

  • Jsp | Ajax + fileupload + json + servlet by many file upload progress bar shows 2009-04-02

    Sales: Jsp | Ajax + fileupload + json + servlet by many file upload progress bar shows demo video and screenshots download 7.55M: Download 1: mail.qq.com / cgi-bin / ftnExs_download Download 2 (javaEye): http://celerity.javaeye.com/topics/download/d1

  • Servlet / JSP Detailed configuration (basic article) 2009-04-12

    Beginners question such as: "How to configure the environment variable" "How to run Servlet" ah? This question too much, now I Started Writing a must-read for beginners in order to have beginners guide! First, download tools: I suggest

  • [Cactus entry] eclipse environment, integration with Cactus and Jetty to test Servlet 2009-05-09

    1) download Cactus-1.8.1 Download commons-codec-1.3 2) decompression Cactus, put under lib add the following jar to the eclipse's classpath: cactus.core.framework.uberjar.javaEE.14-1.8.1.jar commons-httpclient-3.1.jar commons-logging-1.1.jar aspectjrt-1.5