微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Java Spring Boot Rest中从前端处理base64字符串图像

如何解决如何在Java Spring Boot Rest中从前端处理base64字符串图像

有人可以帮助我了解更多有关将base64字符串保存到服务器spring中的文件夹的信息吗? 对于像我这样的新手,我当然不知道将哪种类型的内容保存到服务器的某个文件夹中

我已经在Google中阅读并搜索了很多内容,但不符合我想要的代码或适合我的代码的要求

是在某些文件夹中是保存了base64字符串的.txt吗?还是仅图像jpg或png

我真的想保存为jpg或png到数据库中的某个文件

我使用用于春季启动网络应用程序的reactJs从前端接收了base64字符串

我想用邮递员来处理

POST : http://localhost:1233/api/merchant/createProduct/1 <= this is in postman
{
    "productCategoryName": "product category must be input","productName": "Product Name is required","productDescription": "Product description is required"
    "productimagePicture" : "the base64 string,how this thing save into some folder server as image png or jpg?"
}

然后我的代码是这样的,但是我不知道如何从前端处理base64字符串

@CrossOrigin
@RestController
@RequestMapping("/api/merchant")
public class MerchantController {

@Autowired
private MapValidationErrorService mapValidationErrorService;

@Autowired
private ProductService productService;

@Autowired
private MerchantService merchantService;

    @PostMapping("/createProduct/{merchant_id}")
    public ResponseEntity<?> createNewProduct(@Valid @RequestBody Product product,BindingResult result,@PathVariable Long merchant_id,Principal principal){
        
        ResponseEntity<?> mapError = mapValidationErrorService.MapValidationService(result);
        if(mapError != null) return mapError;
        
        Product product1 = productService.createProduct(merchant_id,product,principal.getName());
        return new ResponseEntity<Product>(product1,HttpStatus.CREATED);
    }
}

这是productService中的代码,这是我的主要问题,类似于之前的邮递员发帖

    public Product createProduct(Long merchant_id,Product product,String merchantName) {

    try {
        Merchant merchant = merchantRepository.findMerchantByUserMerchantUsername(merchantName);
        
        //here the progress logic that i made it Now from some website tutorial
        //product.getProductimagePicture is a String in Product Entity
        //theBase64 string = upImg

        String upImg = uploadImage(product.getProductimagePicture());
        product.setProductimage(the base64 string);


        is it need to set the base64 string i got from json body and save it into Product entity 
        'productimagePicture'      
        and then save the image into some folder in server using function 'uploadImage' as png or jpg only
        



        product.setMerchant(merchant);
        product.setMerchantName(merchant.getMerchantName());
        product.setProductCategory(product.getProductCategory().toLowerCase());
        
        Integer totalProduct = merchant.getTotalProduct();
        totalProduct++;         
        merchant.setTotalProduct(totalProduct);
                
        return productRepository.save(product);
    } catch (Exception e) {
        throw new MerchantNotFoundException("Merchant not found");
    }
}

这是函数uploadImage(),在这一点上我真的很麻烦,非常感谢您为此提供解决方

//String imageVal = i got from product. json in postman
    private String uploadImage(String imageVal) throws Exception {
        
            byte[] decodedBytes = Base64.getDecoder().decode(imageVal);
            String decodedString = new String(decodedBytes);
            
            
//          FileInputStream imagestream = new FileInputStream(imageVal);
//          byte[] imageByte = imagestream.readAllBytes();
//          String imageString = Base64.getEncoder().encodetoString(imageByte);
            
//          byte[] imageByte2 = org.apache.tomcat.util.codec.binary.Base64.decodeBase64(imageVal);
    
            //save file local
            String filePath = Paths.get(uploadDirectory).toString();
            FileWriter fileWriter = new FileWriter(filePath);
            fileWriter.write(decodedString);
            fileWriter.close();
//          imagestream.close();
            
//          new FileOutputStream(filePath).write(decodedString);
            
//          bufferedoutputstream stream = new bufferedoutputstream(new FileOutputStream(new File(filePath)));
//          stream.write(filePath.getBytes());
//          stream.close();
//          
            return decodedString;
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。