1. 程式人生 > >java Excel檔案生成後轉MultipartFile 完成檔案上傳。

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