HttpServletResponse附件下载详解

本文介绍了如何处理浏览器直接打开二进制附件的问题,提供了一种通过HttpServletResponse进行附件下载的解决方案,包括从第三方平台获取附件、设置附件名称和Content-Type,确保浏览器弹出下载框。文章提供相关代码示例,并附带源码下载链接。
摘要由CSDN通过智能技术生成

背景

最近做了一些附件上传下载的功能,附件都是上传第三方平台,例如青云。该章记录一下附件下载。

目的

有一些浏览器在打开第三方附件下载地址的时候,会直接打开文件,看到的都是二进制乱码,而不是弹出下载框,例如火狐浏览器,该实现方式是为了兼容这种情况。

流程

第一步:前端把第三方下载地址传递给服务端,或者服务端自己从业务数据库直接获取。

第二步:前端把附件的名称传递给服务端,或者服务端自己从业务数据库直接获取,或自动生成。

第三步:通过http请求的方式获取附件的二进制流。

第四步:通过HttpServletResponse输出,浏览器会自动匹配,直接弹出下载框。

实现

备注:文章结尾有源码下载地址,下载之后,可直接用eclipse导入maven项目运行。

一)DownloadController对外接口类代码

package com.oysept.attachment.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oysept.attachment.utils.DownloadUtils;
import com.oysept.attachment.utils.HttpUtils;

/**
 * 附件下载controller
 * @author ouyangjun
 * 由于下载时返回一个流给前端,所以不能使用@RestController注解
 */
@Controller
@RequestMapping(value = "/attachment")
public class DownloadController {

    /**
     * 首页测试接口
     * 地址: http://localhost:8080/attachment/download/home
     * @return
     */
    @RequestMapping(value = "/download/home", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String home(){
        return "oysept attachment download home!";
    }
	
    /**
     * 通过第三方下载地址,获取流返回给前端
     * 原因: 有一些浏览器,直接输入第三方下载地址,会直接打开文件,不会直接下载,需要手动在浏览器配置之后才ok
     * 注意: fileUrl和fileName需要urlencode
     */
    @RequestMapping(value = "/download/urlDownload", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public void urlDownload(@RequestParam(value="fileUrl") String fileUrl,
            @RequestParam(value="fileName") String fileName, HttpServletResponse response) {
        if (StringUtils.isEmpty(fileUrl) || StringUtils.isEmpty(fileName)) {
            System.out.println("fileUrl or fileName is null!");
            return;
        }
		
        try {
            // 获取文件类型
            String fileType = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length())
					.toLowerCase();
			
            // 解码
            String decodeFileUrl = URLDecoder.decode(fileUrl, "UTF-8");
            System.out.println("decodeFileUrl: " + decod
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现多附件下载,你可以使用 HttpServletResponse 对象来设置多个附件的响应头。以下是一个示例代码: ```java // 设置响应头为多附件下载 response.setHeader("Content-Disposition", "attachment; filename=attachment1.txt"); response.setHeader("Content-Disposition", "attachment; filename=attachment2.txt"); // 获取文件的输入流 InputStream inputStream1 = new FileInputStream("path/to/attachment1.txt"); InputStream inputStream2 = new FileInputStream("path/to/attachment2.txt"); // 获取响应输出流 OutputStream outputStream = response.getOutputStream(); // 设置缓冲区大小 byte[] buffer = new byte[1024]; int bytesRead; // 将文件内容写入响应输出流 while ((bytesRead = inputStream1.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } inputStream1.close(); while ((bytesRead = inputStream2.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } inputStream2.close(); // 关闭响应输出流 outputStream.close(); ``` 在上面的示例中,我们首先使用 `setHeader()` 方法设置了两个附件的响应头。然后,通过 `FileInputStream` 来获取两个附件的输入流。接下来,我们通过 `getOutputStream()` 方法获取响应的输出流,并将文件内容写入输出流,实现了多附件下载。最后,记得关闭输入流和输出流。 请注意,在实际使用中,你需要根据自己的情况修改文件路径和文件名。此外,你还可以根据需要设置其他响应头参数,如文件类型、文件大小等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值