java Excel文件生成后转MultipartFile 完成文件上传。

由于需求。。

使用了ByteArrayOutputStream和ByteArrayInputStream类.
将XSSFWorkbook 写入ByteArrayOutputStream.然后用ByteArrayOutputStream来转换为字节流.然后再将字节流转换为ByteArrayInputStream …至此,我们就在内存中将excel转换成了输入流…
话不多说,上代码:

   //wb   为 XSSFWorkbook wb = new XSSFWorkbook();//创建一个Workbook
   //由于生成Excel代码太多... 
   ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            wb.write(os);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        byte[] b = os.toByteArray();
        ByteArrayInputStream in = new ByteArrayInputStream(b);
        String  strBase64 = ioToBase64(in);
        MultipartFile file = base64ToMultipart(strBase64);
        
        然后通过MultipartFile完成文件上传。..
// io转base64 
public static String ioToBase64(InputStream in) throws IOException {
        String strBase64 = null;
        try{
             //in.available()//返回文件的字节长度
            byte[] bytes = new byte[in.available()];
            in.read(bytes);
            // 将文件中的内容读入到数组中
            strBase64 = new BASE64Encoder().encode(bytes);   //将字节流数组转换为字符串
            in.close();
        }catch (IOException e) {
                e.printStackTrace();
            }
            return strBase64;
        }


    public static MultipartFile base64ToMultipart(String base64) {
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] b = decoder.decodeBuffer(base64);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            return new BASE64DecodedMultipartFile(b, base64);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
public class BASE64DecodedMultipartFile implements MultipartFile {

    private final byte[] imgContent;
    private final String header;

    public BASE64DecodedMultipartFile(byte[] imgContent, String header) {
        this.imgContent = imgContent;
        this.header = header.split(";")[0];
    }

    @Override
    public String getName() {
        // TODO - implementation depends on your requirements
        return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
    }

    @Override
    public String getOriginalFilename() {
        // TODO - implementation depends on your requirements
        return System.currentTimeMillis() + (int)Math.random() * 10000 + "." + header.split("/")[1];
    }

    @Override
    public String getContentType() {
        // TODO - implementation depends on your requirements
        return header.split(":")[1];
    }

    @Override
    public boolean isEmpty() {
        return imgContent == null || imgContent.length == 0;
    }

    @Override
    public long getSize() {
        return imgContent.length;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return imgContent;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(imgContent);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        new FileOutputStream(dest).write(imgContent);
    }
}

参考博客:
https://blog.csdn.net/u011109420/article/details/51330677
https://blog.csdn.net/qq_37861937/article/details/78218547
https://blog.csdn.net/izb2008/article/details/79623234

猜你喜欢

转载自blog.csdn.net/huang__2/article/details/83896419