文件下载

@RequestMapping(value = "/downloadAndroid")
@ResponseBody
public void downloadAndroid(HttpServletResponse response, HttpServletRequest request){HashMap<String, String> paramsMap = QueryToolUtil.queryUtil(request);String saasId = request.getParameter("saasId");String businessType = request.getParameter("businessType");String apkPath = this.getClass().getClassLoader().getResource("/").getPath()+"/appdownload/app-release.apk";//要下载文件的相对位置//new 一个apk的文件对象File file = new File(apkPath);try {if(file.exists()){// 以流的形式下载文件//先以输入流把文件输入到buffer中,再以输出流的形式输出InputStream fis = new BufferedInputStream(new FileInputStream(apkPath));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();// 清空responseresponse.reset();// 设置response的Headerresponse.setHeader("content-type", "application/vnd.android.package-archive");response.addHeader("Content-Disposition", "attachment;filename=" + file.getName());response.addHeader("Content-Length", "" + file.length());OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");toClient.write(buffer);toClient.flush();toClient.close();}}catch(Exception e) {System.out.println("下载文件错误"+e.getMessage());}
}

下载zip文件

@RequestMapping(value = "/downloadExample")
@ResponseBody
public void downloadExample(HttpServletResponse response, HttpServletRequest request){String path = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "//template//cashloanicon.zip";File file = new File(path);try {// 以流的形式下载文件。InputStream fis = new BufferedInputStream(new FileInputStream(path));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();// 清空responseresponse.reset();OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");//如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("个人小额借贷示例.zip", "UTF-8"));toClient.write(buffer);toClient.flush();toClient.close();} catch (IOException ex) {ex.printStackTrace();} finally {try {File f = new File(file.getPath());} catch (Exception e) {e.printStackTrace();}}
}

根据地址生成二维码图片

@RequestMapping(value="/createCodeAndUpload",method= RequestMethod.POST)
@ResponseBody
public JsonResult createCodeAndUpload(HttpServletRequest request){String url = request.getParameter("url");String path = FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + "testQrcode";String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";final int BLACK = 0xFF000000;final int WHITE = 0xFFFFFFFF;File file = null;try {Map<EncodeHintType, String> hints = new HashMap<>();hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);file = new File(path, fileName);if (file.exists() || ((file.getParentFile().exists() || file.getParentFile().mkdirs()) && file.createNewFile())) {int width = bitMatrix.getWidth();int height = bitMatrix.getHeight();BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);}}if (!ImageIO.write(image, "jpg", file)) {throw new IOException("Could not write an image of format " + "jpg" + " to " + file);}System.out.println("搞定:" + file);}} catch (Exception e) {e.printStackTrace();}if(file == null){return new JsonResult().setMsg("生成二维码失败,请稍后重试!").setSuccess(false);}
}

转载于:https://www.cnblogs.com/whatarewords/p/10723462.html

下载文件、根据链接生成二维码相关推荐

  1. 百万前端之js通过链接生成二维码可以保存下载复制

    在前端工作中,会遇到很多邀请好友的需求,这个时候就需要前端对需求进行实现了. 最终效果: 这个是做好以后的成品,根据链接生成二维码,提供保存二维码和复制链接功能,纯前端实现.话不多说直接上代码 htm ...

  2. js前端根据链接生成二维码并转成图片下载

    js前端根据链接生成二维码并转成图片下载 依赖于jquery.jquery.qrcode.min.js 1.html <div class="qrcode"></ ...

  3. dom 生成图片和链接生成二维码

    1. dom 生成图片 此处使用的是 html2canvas 插件 和canvas2image插件 html2canvas 方法的第二个入参还有很多选项可以配置,详情请看这里这里 html 1 < ...

  4. java生成二维码到文件,java生成二维码转成BASE64

    java生成二维码到文件,java生成二维码转成BASE64 如题,利用java和第三方库,把指定的字符串生成二维码,并且把二维码保存成图片,转换成BASE64格式. 需要的jar文件: packag ...

  5. java 根据指定链接生成二维码

    需求: 就是需要把一个报告网址链接生成二维码,然后渲染在报告中,扫描二维码的时候能访问到这个报告,效果就达到了 1.首先提前需要添加的maven依赖 <!-- https://mvnreposi ...

  6. 链接生成二维码( QRCode )

    Vue中链接生成二维码 安装插件 sudo npm install qrcodejs2 --save 页面中引入 import QRCode from "qrcodejs2"; c ...

  7. iOS常用功能 - 根据链接生成二维码图片

    根据链接生成二维码 1 -(void)generatingTwoDimensionalCode { 2 3 // 创建过滤器 4 CIFilter *filter = [CIFilter filter ...

  8. js实现将链接生成二维码,并对生成的二维码转换为图片,右击可保存至本地

    在vue项目中,实现将链接生成二维码:可识别跳转,将生成的二维码可转换成图片,并可保存至本地.具体操作步骤如下: 1.安装及引用 首先,实现这个功能需要使用QRCode和html2canvas,所以在 ...

  9. Python 二维码的读取与生成:使用链接生成二维码、读取二维码里的链接

    Python 二维码的读取与生成演示 ① 使用链接生成二维码 ② 读取二维码里的链接 [ 文章推荐 ] Python 绘制中国地图:使用 pyecharts 最新版本绘制中国地图实例详解,个性化地图定 ...

最新文章

  1. python数组越界_python 整数越界问题详解
  2. centos lamp 连接mysql_centOS下lamp安装
  3. mysql源码如何解析where字句_MySQL解析器源码分析--对select语句中子查询处理逻辑的分析(一)...
  4. python自动化测试看什么书-Python自动化测试入门,看这一篇就足以
  5. linux下装jdk以及failed /usr/local/jdk1.6.0_10/jre/lib/i386/client/libjvm.so,
  6. .NET深入实战系列—Linq to Sql进阶
  7. 标准C程序设计七---66
  8. python数组追加_python向数组追加数据库
  9. 深度学习《CNN架构续篇 - BatchNorm》
  10. office右键菜单修复_自从有了这个工具,我的Office操作已经快到慢不下来了!
  11. JavaScript 运算符优先级 - 汇总表
  12. 软件项目管理系统-进度编辑
  13. 图论--哈密顿路与欧拉路
  14. 中望3d快捷键命令大全_3d快捷键怎么设置|中望3D快捷键设置
  15. android logo颜色渐变,华为悄然更新品牌Logo:无渐变色,更加扁平化
  16. Robotframework(三)常用API介绍
  17. linux编辑vim指令,Linux系统文本编辑器vim指令大全
  18. flutter 图片编辑矩形 editBox
  19. 秒懂云通信:如何使用阿里云号码认证服务(小白指南)
  20. android M权限问题

热门文章

  1. 常见java空指针异常
  2. 一文搞懂 STL 中 deque 与 hashtab 的底层实现
  3. 四川省中小学计算机免试,四川省教育厅关于转发教育部关于进一步做好小学升入初中免试就近入学工作的实施意见的通知...
  4. So Easy京东商城
  5. 免费使用腾讯云每天定时签到京东领取京豆
  6. 带中文字库的12864驱动笔记
  7. 【组合数学】组合数学简介 ( 组合思想 2 : 数学归纳法 | 数学归纳法推广 | 多重归纳思想 )
  8. linux ipp,IPP 文件扩展名: 它是什么以及如何打开它?
  9. MySQL查询近12个月数据,无则补0
  10. [ 云原生之谜 ] 云原生背景 定义 相关技术详解?