File转MultipartFile


    commons-fileupload
    commons-fileupload
    1.3.2
    /***
     * @MethodName getMultipartFile
     * @Description  File转MultipartFile
     * @param file File类
     * @param fieldName  生成的文件名称
     * @return org.apache.commons.fileupload.FileItem
     *
     * @Author syh
     * @Date 2023/4/14 15:01
     */
    public static FileItem getMultipartFile(File file, String fieldName){
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem item = factory.createItem(fieldName, "text/plain", true, file.getName());
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        try {
            FileInputStream fis = new FileInputStream(file);
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return item;
    }
FileItem fileItem  = FileUploading.getMultipartFile(file, System.currentTimeMillis() + zcLidInfo.getId());
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

你可能感兴趣的:(java开发模板,java,数据库,mysql)