Java通过网络图片之地址,下载到服务器,打包到本地

本文介绍了一种通过设置HTTP响应头来解决中文文件名乱码的方法,并提供了一个具体的示例,包括如何使用a标签调用以及处理中文乱码的具体实现。

a标签调用,处理了中文乱码问题

@RequestMapping("/downloadTableQrcode")

    public void downloadTableQrcode(HttpServletRequest request, HttpServletResponse response,@RequestParam String name,@RequestParam String url) throws Exception{
        try {
            response.setContentType("application/zip");
            String fileName = new String(name.getBytes(),"ISO8859-1"); //正确,不发生乱码
            response.setHeader("Content-Disposition", "attachment; filename="+fileName+".jpg");
            javax.servlet.ServletOutputStream out = response.getOutputStream();
            DownloadPicFromURL.downloadPicture(url,request.getSession().getServletContext().getRealPath("/")+"downloadTableQrcode.jpg");
            File file = null;
            file = new File (request.getSession().getServletContext().getRealPath("/")+"downloadTableQrcode.jpg");
            InputStream inputStream = new FileInputStream(file);
            int b = 0;
            byte[] buffer = new byte[1000000];
            while (b != -1) {
                b = inputStream.read(buffer);
                if(b!=-1) out.write(buffer, 0, b);
            }
            inputStream.close();
            out.close();
            out.flush();
        }catch(Exception e){e.printStackTrace();
        }finally {
        }

    }

-------------------------工具类----------------------

//链接url下载图片
    public static void downloadPicture(String urlList,String path) {
        URL url = null;
        try {
            url = new URL(urlList);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());


            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();


            byte[] buffer = new byte[1024];
            int length;


            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小达哥的垃圾桶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值