`
Luckydan
  • 浏览: 8346 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Servlet Download Flie Function

 
阅读更多
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

  String fileName = req.getParameter("fileName");
  String filePath = "c:/file folder/... ";
  File file = new File(filePath);
  int length = 0;
  ServletOutputStream out = resp.getOutputStream();
  ServletContext context = getServletConfig().getServletContext();
  String mimetype = context.getMimeType(filePath);
  if (mimetype == null) {
mimetype = "application/octet-stream";
  }
  resp.setContentType(mimetype);
  int fileSize = (int) file.length()
  if (fileSize == 0) {
     resp.setContentLength(fileSize);
  }else {
     // some kind of file with empty content, if you dont specify the length, it will simply return a unkown file
     resp.setContentLength(1);
  }
//
  resp.setHeader("Content-Disposition", "attachment; filename=\""
  + fileName + "\"");
  byte[] byteBuffer = new byte[BUFFER_SIZE];
  DataInputStream in = new DataInputStream(new FileInputStream(file));
  while ((in != null) && ((length = in.read(byteBuffer)) != -1)) {
out.write(byteBuffer, 0, length);
  }
  in.close();
  out.flush();
  out.close();
}
分享到:
评论

相关推荐

    websocket java 实现例子

    在Java中实现WebSocket,我们可以借助于Tomcat 8这样的Servlet容器以及JDK 1.8提供的API。下面将详细介绍WebSocket在Java中的实现及其与Tomcat 8和JDK 1.8的结合使用。 首先,我们需要了解WebSocket的基本概念。...

    JavaServlet的文件上传和下载实现方法

    $("a[id=downLoad]").click(function() { window.location.href = path.deployPath + "/fileDown"; }); ``` 服务器端发送文件 在服务器端,需要使用JavaServlet来发送文件。可以使用Java的inputStream和...

    javascript上传下载代码

    function downloadData(data, filename) { var blob = new Blob([data], {type: 'application/octet-stream'}); var url = URL.createObjectURL(blob); var link = document.createElement('a'); link.href = ...

    java文件上传、删除、下载的通用方法总结.pdf

    @WebServlet("/download") public class DownloadServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ...

    vue上传和下载前后台开发

    changeFile: function(event) { this.file = event.target.files[0]; var pos = $("#path").val().lastIndexOf("\\"); var fileName = $("#path").val().substring(pos + 1); document.getElementById("excel-...

    Java 上传下载

    File uploadedFile = new File("/path/to/save/files/" + fileName); item.write(uploadedFile); } } // 保存文件信息到数据库或其他操作 return mapping.findForward("success"); } ``` 下载部分,我们...

    在程序中实现上传附件的代码

    <a TARGET="#" href="<%=path%>/common/file/download.action?fileId=<ww:property value="%{id}" />"> <span style="width:10%">  <img align="middle" onclick="removeFile(this,'...

    AJAX框架DWR简单应用.pdf

    这可以通过访问[Apache Log4j官网](https://logging.apache.org/log4j/2.x/download.html)下载相应的JAR包。 3. **配置Log4j属性文件**: 在项目的资源文件夹中创建或复制一个`log4j.properties`文件,内容如下: `...

    MVC框架中用到的实现上传下载的Java类

    <input type="file" name="file" /> 上传 $(document).ready(function() { $("#uploadForm").on("submit", function(event) { event.preventDefault(); var formData = new FormData(this); $.ajax({ url:...

    java后台批量下载文件并压缩成zip下载的方法

    for (File file : fileList) { // 下载文件 InputStream in = new FileInputStream(file); // 添加到zip文件中 zipStream.putNextEntry(new ZipEntry(file.getName())); IOUtils.copy(in, zipStream); in....

    html五实现文档上传下载代码

    document.getElementById('fileInput').addEventListener('change', function(event) { var files = event.target.files; // 处理files对象,如上传到服务器 }); ``` 接下来,我们需要处理文件上传。在...

    利用 FormData 对象和 Spring MVC 配合实现Ajax文件下载功能

    public StreamingResponseBody download(@RequestParam("userfile1") MultipartFile file1, @RequestParam("userfile2") MultipartFile file2) { // 检查文件是否为空,大小等 if (file1.isEmpty() || file2....

    外文翻译 stus MVC

    The file struts-config.xml instructs ActionServlet on how to use the extended classes. There are several advantages to this approach: • The entire logical flow of the application is in a ...

    flex中上传与下载的例子

    import javax.servlet.http.HttpServletRequest; import org.springframework.web.multipart.MultipartFile; public class FileUploadAction { public void handleUpload(HttpServletRequest request) { ...

    flex文件上传下载

    1. 直接下载:通过 `FileReference` 类的 `download()` 方法,可以从 URL 下载文件到本地。用户可以选择保存文件的位置,或者让系统自动保存。 2. 浏览器下载:另一种方式是创建一个指向下载链接的 `URLRequest`,...

    自己动手写一个java版简单云相册

    2. **文件下载**:Servlet监听/download路径的请求,根据请求参数找到对应的文件,然后返回文件内容,浏览器将其作为下载处理。 3. **图片浏览**:用户可以在页面上查看所有图片的列表,点击链接可以跳转到图片...

    Python Cookbook, 2nd Edition

    Adapting a File-like Object to a True File Object Recipe 2.16. Walking Directory Trees Recipe 2.17. Swapping One File Extension for Another Throughout a Directory Tree Recipe 2.18. Finding a ...

    图片上传插件jquery.uploadify详解

    `jQuery.uploadify` 插件可以从其官方网站(http://www.uploadify.com/download/)下载。这个插件简化了图片上传的实现,提供了一整套可配置的选项和事件处理机制,使得在网页应用中集成图片上传功能变得更加便捷。

    ajax下载smartupload的内容无法显示汉字的解决方法

    function getFile(){ // 不推荐使用$.get进行文件下载 // $.get("<%=basePath%>LoadFileServlet","",function(data){ alert(data); }); } // 更推荐的做法是使用超链接直接触发下载 <a href="<%=basePath%>...

Global site tag (gtag.js) - Google Analytics