java在浏览器弹框读取本地文件另存

java在浏览器弹框读取本地文件另存

public static void download(HttpServletResponse response, String filename) throws IOException {
		// 获取服务器文件
		File file = new File(filename);

		InputStream ins = new FileInputStream(file);
		OutputStream os = null;
		/* 设置文件ContentType类型,这样设置,会自动判断下载文件类型 */
		response.setContentType("multipart/form-data");
		/* 设置文件头:最后一个参数是设置下载文件名 */
		response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
		try {
			os = response.getOutputStream();
			byte[] b = new byte[1024];
			int len;
			while ((len = ins.read(b)) > 0) {
				os.write(b, 0, len);
			}
			os.flush();

		} catch (IOException ioe) {
			ioe.printStackTrace();
		} finally {
			os.close();
			ins.close();
		}
	}
 /**
     * 下载模板
     *
     * @param httpServletRequest
     * @param response
     * @param name               文件名
     * @param path               文件路径
     * @return
     * @throws Exception
     */
    public ServletOutputStream downFileForMoulds(HttpServletRequest httpServletRequest, HttpServletResponse response, String name, String path) throws Exception {

        File file = null;
        file = new File(path);
        if (!file.isFile()) {
            file = new File(path, name);
        }

        //下载显示的文件名,解决中文名称乱码问题
        String downloadFielName = null;
        if (httpServletRequest.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
            downloadFielName = new String(name.getBytes("UTF-8"), "ISO8859-1"); // firefox浏览器
        } else if (httpServletRequest.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0 ||
                httpServletRequest.getHeader("User-Agent").indexOf("like Gecko") > 0) {
            downloadFielName = URLEncoder.encode(name, "UTF-8");// IE浏览器
        } else if (httpServletRequest.getHeader("User-Agent").toUpperCase().indexOf("CHROME") > 0) {
            downloadFielName = new String(name.getBytes("UTF-8"), "ISO8859-1");// 谷歌
        }
        response.setHeader("Content-Disposition", "attachment; filename=" + downloadFielName);
        ServletOutputStream output = null;  //定义了一个输出流,用于由Servlet向客户发送二进制数据

        BufferedInputStream br = null;
        try {
            br = new BufferedInputStream(new FileInputStream(file));

            byte[] buf = new byte[1024];
            int len = 0;
            output = response.getOutputStream();
            while ((len = br.read(buf)) > 0) {
                output.write(buf, 0, len);
            }
        } catch (Exception e) {
            log.error("文件下载异常", e);
            throw new Exception("文件下载错误,请联系管理员!");
        } finally {
            if (br != null) {
                br.close();
            }
            if (output != null) {
                output.close();
            }
        }
        return output;
    }

两种基本相同,加了一个浏览器校验

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值