将图像上传到Google存储桶后,不再将其识别为图像

问题描述

我在SpringBoot中使用Vaadin 14。我正在编写一些将图片上传到Google存储桶的软件。上载本身可以正常工作,但进入存储桶的文件不再是图像。我在几周前编写了代码,然后仍然有效,没有任何变化,但现在不再起作用。我使用InputStream将其上传到存储桶,我从Vaadin上传组件获取流。这是代码

从vaadin组件上载到memoryBuffer

public CreateKidView(@Autowired KidService kidService) {
        this.kidService = kidService;

        TextField kidFirstName = new TextField("Voornaam");
        TextField kidLastName = new TextField("Achternaam");
        DatePicker kidBirthDate = new DatePicker("GeboorteDatum");
        TextField kidMedication = new TextField("Medicatie");
        MemoryBuffer memoryBuffer = new MemoryBuffer();
        Upload imageUpload = new Upload(memoryBuffer);

        Button button = new Button("Kind toevoegen",buttonClickEvent -> {
                    saveKidAndNavigate(kidFirstName.getValue(),kidLastName.getValue(),kidBirthDate.getValue(),kidMedication.getValue(),memoryBuffer.getInputStream());
                }
        );

        button.addThemeVariants(ButtonVariant.LUMO_PRIMARY);

        addClassName("centered-content");

        add(kidFirstName,kidLastName,kidBirthDate,kidMedication,imageUpload,button);
    }

然后,我将输入流传递给其他几个类,直到到达这里,我才对其进行任何处理:设置contentType:“ image / jpeg”,并将其传递给我的bucketService以上传输入流。 / p>

public void addImageToPerson(Person person,String imageName,InputStream inputStream) {
        String image = bucketService.uploadFileToBucket(imageName,"image/jpeg",inputStream);
        ......
    }
@Slf4j
@Service
public class BucketService {

    private final String companyName;
    private final Storage storage;

    @Autowired
    public BucketService(@Value("${company.name}")String companyName,@Value("${gc.project.name}")String gcProjectName) {
        this.companyName = companyName;

        storage = StorageOptions.newBuilder().setProjectId(gcProjectName).build().getService();
    }

    public String uploadFileToBucket(String fileName,String contentType,InputStream inputStream) {
        try {
            BlobId blobId = BlobId.of(companyName,fileName);
            BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
            storage.create(blobInfo,IOUtils.toByteArray(inputStream));
            return fileName;
        } catch (StorageException | IOException e) {
            log.error("could not save image: {}",e.getMessage());
            return "oops";
        }
    }
}

它曾经可以正常工作,此后没有代码发生变化,因此我猜测google发生了一些变化。我想知道你们中是否有人可以尝试或知道已经改变的事情。

我还编写了一些代码来测试inputstream本身是否有效,我在屏幕上显示从内存中获取的图像:

enter image description here

预先感谢:)

-编辑

经过更多调查,我发现inputstream产生的byte []与文件本身产生的byteArray不同:

byte[] bytes = IOUtils.toByteArray(inputStream);
byte[] content = Files.readAllBytes(Paths.get("/Users/BartDuwez/Desktop/avatars-000338105915-u01ky6-t500x500.jpg"));
storage.create(blobInfo,content);

相差1个字节:第一个字节。

字节:-40,-1,-30,...

内容:-1,-40,-1,-30,...

我对此一无所知。 要清除,byte []内容会在存储桶中正确显示图像!

当我检查inputStream中的buf时,有一个byte [],该byte []与内容byte []的内部相同

如果我在调试器中这样做:

storage.create(blobInfo,((ByteArrayInputStream) inputStream).buf);

文件被上传并且可以工作:但是.buf受保护,所以我不能在我的代码中使用它。这使得将输入流转换为byte []

时似乎出现了问题。

知道有什么可以帮助我解决这个问题的人吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...