上传excel 通过url下载文件

阅读更多
/**
     * 下载图片
     */
    @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 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 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 rows = ExcelReader.read(wb, 0, 0);
        Assert.isTrue(!rows.isEmpty(), ErrorEnum.参数不正确, "上传数据不能为空");
        Iterator iterator = rows.iterator();
        ExcelRow head = iterator.next();
        boolean flag = validHead(head, DOWNLOAD_IMAGE);
        Assert.isTrue(flag, ErrorEnum.参数不正确, "格式不正确,请下载最新格式");
        Map 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(categoryPingxing==null){
//                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("\t");
        }
        String str = StringUtils.removeEndIgnoreCase(sb.toString(), "\t");
        if (head.equals(str)) {
            flag = true;
        }
        return flag;
    }

你可能感兴趣的:(上传excel 通过url下载文件)