• 上传excel 通过url下载文件 java


    https://blog.csdn.net/zktsx/article/details/84903504

    /
    * 下载图片
    /
    @RequestMapping("download/picture.json")
    @ResponseBody
    public void downLoadPicture(HttpServletResponse response) throws IOException {
    // String date = DateUtils.getCurrentDate().replaceAll("-", "").substring(2);
    // StringBuilder dis = new StringBuilder("attachment");
    // dis.append(";fileName=").append(new String(date.getBytes("UTF8"), "ISO-8859-1")); // RFC 2184
    // dis.append(";fileName
    =UTF-8''"); // RFC 5987
    // response.setHeader("Content-Disposition", dis.toString());
    //response.setHeader("Content-Encoding", "gzip");
    // validExcel(file);

    // String downloadFilename = "中文.zip";//文件的名称
    // downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
    // response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
    // response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);
    Map<String, String> urlMap = exportExcelBiz.readExcle();
    //
    // ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
    for (String nums : urlMap.keySet()) {
    String url = urlMap.get(nums);
    downloadImage(nums, url);
    }
    // zos.flush();
    // zos.close();
    }

    /

    * 验证文件
    */
    private void validExcel(MultipartFile file) {
    Assert.isFalse(file == null || file.isEmpty(), ErrorEnum.参数不正确, "文件不能未空");
    String extension = FilenameUtils.getExtension(file.getOriginalFilename());
    Assert.isTrue("xls".equals(extension) || "xlsx".equals(extension), ErrorEnum.参数不正确, "文件格式必须为Excel");
    }

    private void downloadImage(String nums, String url) {
    String fileName;
    if (url == null) {
    url = "http://127.0.0.1:7755/no.jpg";
    fileName = "no" + nums + ".jpg";
    } else {
    fileName = nums + ".jpg";
    }
    try {
    File file = new File("image");
    if (!file.exists()) {
    file.mkdirs();
    }
    URL url1 = new URL(url);
    InputStream in = url1.openConnection().getInputStream();
    Image img = ImageIO.read(in);
    BufferedImage tag = new BufferedImage(550, 550, BufferedImage.TYPE_INT_RGB);
    tag.getGraphics().drawImage(img, 0, 0, 550, 550, null);
    OutputStream os = new FileOutputStream(file.getPath() + "/" + fileName);//内存流
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
    encoder.encode(tag); // 近JPEG编码
    IOUtils.copy(in, os);
    in.close();
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /
    *读取excel
    */
    public Map<String, String> readExcle() throws IOException {
    final String path = "/xls/BJ.xls";
    // String fileName = file.getOriginalFilename();
    // Workbook wb = ExcelLoader.load(file.getInputStream(), fileName);
    Workbook wb = ExcelLoader.load(this.getClass().getResourceAsStream(path));
    List<ExcelRow> rows = ExcelReader.read(wb, 0, 0);
    Assert.isTrue(!rows.isEmpty(), ErrorEnum.参数不正确, "上传数据不能为空");
    Iterator<ExcelRow> iterator = rows.iterator();
    ExcelRow head = iterator.next();
    boolean flag = validHead(head, DOWNLOAD_IMAGE);
    Assert.isTrue(flag, ErrorEnum.参数不正确, "格式不正确,请下载最新格式");
    Map<String, String> urlList = new HashMap<>();
    while (iterator.hasNext()) {
    ExcelRow row = iterator.next();
    String numberInner = row.get("A").trim();
    String number = row.get("B").trim();
    String cateName = row.get("C").trim();
    if(Integer.valueOf(number)10){
    continue;
    }
    if (numberInner == null) {
    urlList.put(number, null);
    continue;

    }
    // PxCategoryPingxing categoryPingxing;
    // categoryPingxing = categoryPingxingMapper.selectByName(cateName);
    // if(categoryPingxingnull){
    // categoryPingxing = categoryPingxingMapper.selectByNameFuzzy(cateName);
    // }
    // if(categoryPingxing==null){
    // urlList.put(number, "name error");
    // continue;
    // }
    // Long categoryId = categoryPingxing.getId();
    String url = packetOutMapper.selectItemImageUrl(numberInner,cateName);
    urlList.put(number,url);
    }
    return urlList;
    }

    /

    *验证头部
    */
    public boolean validHead(ExcelRow row, String head) {
    boolean flag = false;
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < row.size(); i++) {
    sb.append(row.get(i)).append(" ");
    }
    String str = StringUtils.removeEndIgnoreCase(sb.toString(), " ");
    if (head.equals(str)) {
    flag = true;
    }
    return flag;
    }

  • 相关阅读:
    IL汇编语言介绍(译)
    开源搜索框架Lucene学习系列
    【转】autoHeight为true的时候,autoScroll为true就不起作用了
    [转]反注册 Regsvr32命令应用大全
    [转]sun.misc.BASE64Encoder找不到的解决方法
    mysql数据库导入导出
    【转】MySQL 与MS SQL Server数据库使用多表关联Update时语法的区别
    CRT detected that the application wrote to memory after end of heap buffer
    魔兽争霸窗口化
    GROUP_CONCAT函数
  • 原文地址:https://www.cnblogs.com/sunny3158/p/14305134.html
Copyright © 2020-2023  润新知