文件下載
@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); } }