java下载字符串

7 篇文章 0 订阅
6 篇文章 0 订阅
  • 依赖 
 <dependency>
     <groupId>commons-io</groupId>
     <artifactId>commons-io</artifactId>
     <version>1.3.2</version>
 </dependency>
  • 工具类 

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * 下载工具类
 * @author czs
 * @date 2020-2-22 11:02:54
 */
public class DownloadUtils {

    /**
     * 根据字符串下载内容
     *
     * @param request  request
     * @param response response
     * @param fileName 下载的文件名
     * @param content  字符串内容
     * @throws IOException
     */
    public static void downloadByStringContent(HttpServletRequest request,
                                               HttpServletResponse response,
                                               String fileName, String content)
            throws IOException {
        //设置向浏览器端传送的文件格式
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
            // firefox浏览器
            fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
        } else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
			// IE浏览器
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } else if (request.getHeader("User-Agent").toUpperCase().indexOf("CHROME") > 0) {
            // 谷歌
            fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
        }
        response.setHeader("Content-disposition", "attachment; filename=" + fileName);
        BufferedInputStream inp = null;
        OutputStream out = response.getOutputStream();
        try {
            inp = new BufferedInputStream(new ByteArrayInputStream(content.getBytes("utf-8")));
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = inp.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inp != null) {
                inp.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

}
  • Controller
@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/exportKtrAndKjb")
public void exportKtrAndKjb(@RequestParam String path, @RequestParam String type, HttpServletRequest request, HttpServletResponse response) throws Exception {
    String xml = xxxService.getXmlByXXX(path);
    String fileName = xxxService.getFileNameByXml(xml);
    DownloadUtils.downloadByStringContent(request, response, fileName, xml);
}
  • JavaScript
window.location.href= 'repository/exportKtrAndKjb.do?path='+ path + '&type=transformation';

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值