栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java后端实现下载功能,下载到用户浏览器(解决浏览器中文转义问题)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java后端实现下载功能,下载到用户浏览器(解决浏览器中文转义问题)

前段时间,项目需要做一个下载的功能,是直接从公司服务器下载文件到本地,

下载的url已经保存到数据库了,但是前端不知道因为什么原因说出bug了,实现不了,让后端看看能不

能帮忙实现一下.

听到后到网上找了一些java实现下载的资料,但是发现大部分都是下载到自己的电脑本地,不能实现

用户下载到自己的电脑上.找了一天终于找到了解决的方法 ,并且解决下载路径中包含中文问题(ps:浏览器请求路径时会把中文转义)

import com.gbpi.digitization.util.JsonResult;
import com.gbpi.digitization.util.RequiredPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

@RestController
@Api(value = "文件下载到浏览器接口", description = "文件下载到浏览器接口")
public class Download {

   
    @GetMapping("/download")
    @ApiOperation(value = "文件下载", notes = "参数:无")
    public void findOperatorCharBar2(HttpServletResponse response, String path, String docName) throws IOException {
            toDownload(response, path, docName);

    }





    public static void toDownload(HttpServletResponse response, String path, String docName) {
        ServletOutputStream out = null;
        InputStream inputStream = null;

        try {
            //中文转义,浏览器中文需要转义,否则报400参数错误
            int lastIndexOf = path.lastIndexOf('/');
            String fileName = path.substring(lastIndexOf + 1);
            // 转义关键代码
            String newFileName = URLEncoder.encode(fileName, "utf-8");
            String subUrl = path.substring(0,lastIndexOf + 1);
            String newUrl = subUrl + newFileName;

            URL url = new URL(newUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();



            conn.setConnectTimeout(3 * 1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            inputStream = conn.getInputStream();
            
            int len = 0;
            // 输出 下载的响应头,如果下载的文件是中文名,文件名需要经过url编码
            response.setContentType("application/x-download");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(docName, "UTF-8"));
            response.setHeader("Cache-Control", "no-cache");
            out = response.getOutputStream();
            byte[] buffer = new byte[1024];
            while ((len = inputStream.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.flush();
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/354166.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号