我想使用Google Cloud Storage存储上传的静态图片。大多数样本会上传txt或部分文件。
我检查了很多来自Google的文档,但仍然不知道如何上传 缓冲的图像或图像。
我要上传的img全部在本地编辑。因此,尽管它们以多部分的形式出现,但最终它们变成了缓冲图像。
以下代码的结果为空指针:
@PostMapping("/productImages")
public Object add(@RequestParam("pid") int pid, @RequestParam("type") String type, MultipartFile image, HttpServletRequest request) throws Exception {
String fileName = image.getOriginalFilename();
String mimeType = request.getServletContext().getMimeType(fileName);
if (!mimeType.startsWith("image/")) {
return 0;
}
ProductImage bean = new ProductImage();
Product product = productService.get(pid);
bean.setProduct(product);
bean.setType(type);
String folder = "/img/";
if(type_single.equals(bean.getType())){
folder +="productSingle/";
}
else{
folder +="productDetail/";
}
// BufferedImage bufferedImage = convertToImage(image);
File imageFolder= new File(request.getServletContext().getRealPath(folder));
System.out.println(request.getServletContext().getRealPath(folder));
File file = new File(imageFolder,bean.getId()+".jpg");
String descName = folder+bean.getId()+".jpg";
// if(!file.getParentFile().exists())
// file.getParentFile().mkdirs();
try {
// image.transferTo(file);
BufferedImage img = ImageUtil.change2jpg(file);
System.out.println(descName);
System.out.println(image.getOriginalFilename());
googleCloudStorage.uploadFile(img,descName);
productImageService.add(bean);
// ImageIO.write(img, "jpg", file);
} catch (IOException e) {
e.printStackTrace();
}
GoogleCloudStorage类:
public class GoogleCloudStorage {
private Storage storage;
private Bucket bucket;
String pathToConfig = "/google_auth.json";
String projectId = "gogogo";
String bucketName = "gogog-image";
public GoogleCloudStorage() throws IOException {
Credentials credentials = GoogleCredentials.fromStream(new FileInputStream(pathToConfig));
storage = StorageOptions.newBuilder().setCredentials(credentials).setProjectId(projectId).build().getService();
bucket = storage.get(bucketName);
}
public String uploadFile(BufferedImage bufferedImage, String fileName) throws IOException{
System.out.println("123");
ByteArrayOutputStream os = new ByteArrayOutputStream();
BlobInfo blobInfo = storage.create(BlobInfo.newBuilder(bucketName,fileName).setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER)))).build(),bufferedImage.());
return blobInfo.getMediaLink();
}
ImageUtil
public static BufferedImage change2jpg(File f) {
try {
Image i = Toolkit.getDefaultToolkit().createImage(f.getAbsolutePath());
PixelGrabber pg = new PixelGrabber(i, 0, 0, -1, -1, true);
pg.grabPixels();
int width = pg.getWidth(), height = pg.getHeight();
final int[] RGB_MASKS = { 0xFF0000, 0xFF00, 0xFF };
final ColorModel RGB_OPAQUE = new DirectColorModel(32, RGB_MASKS[0], RGB_MASKS[1], RGB_MASKS[2]);
DataBuffer buffer = new DataBufferInt((int[]) pg.getPixels(), pg.getWidth() * pg.getHeight());
WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, RGB_MASKS, null);
BufferedImage img = new BufferedImage(RGB_OPAQUE, raster, false, null);
return img;
} catch (InterruptedException e) {
e.printStackTrace();
return null;
}
}