java 里 字符串转url url转file file 转MultipartFile

代码:

try {            
                     URL videoImagePathUrl = new URL("file:///"+videoImagePath);    //一定要加    file:///
                     File f=new File(videoImagePathUrl.toURI());
                    FileItem fileItem = new DiskFileItem("mainFile", Files.probeContentType(f.toPath()), false, f.getName(), (int) f.length(), f.getParentFile());
                    try (InputStream input = new FileInputStream(f); OutputStream os = fileItem.getOutputStream();) {
                        IOUtils.copy(input, os);
                        MultipartFile mulFile = new CommonsMultipartFile(fileItem);
                        ProductImage productImage = productImageService.generate(mulFile);
                        productImage.setOrder(0);
                        JSONObject jsonObject=new JSONObject();
                        jsonObject.append("source", productImage.getSource());
                        jsonObject.append("large", productImage.getLarge());
                        jsonObject.append("medium", productImage.getMedium());
                        jsonObject.append("thumbnail", productImage.getThumbnail());
                        jsonObject.append("order",productImage.getOrder());
                        videoImageUrl=jsonObject.toString();
                        System.out.println(productImage.toString());
                    }
                 }catch (MalformedURLException e) {   
                     e.printStackTrace();      
            }

猜你喜欢

转载自blog.csdn.net/qq_34827263/article/details/85247252