inputStream 转换 MultipartFile

/**
 * inputStream 转换 MultipartFile
 *
 * @param inputStream inputStream
 * @param fileName    fileName
 * @return FileItem
 */
public static MultipartFile createFileItem(InputStream inputStream, String fileName) {
    FileItemFactory factory = new DiskFileItemFactory(16, null);
    String textFieldName = "file";
    FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    OutputStream os = null;
    //使用输出流输出输入流的字节
    try {
        os = item.getOutputStream();
        while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        inputStream.close();
    } catch (IOException e) {
        log.error("Stream copy exception", e);
        throw new IllegalArgumentException("文件上传失败");
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                log.error("Stream close exception", e);
            }
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                log.error("Stream close exception", e);
            }
        }
    }
    return new CommonsMultipartFile(item);
}