下载文件、根据链接生成二维码
文件下载
@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(); // 清空response response.reset(); // 设置response的Header response.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(); // 清空response response.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); } }