gpt4 book ai didi

java - 通过Google应用程序引擎(JAVA)将文件(图像或视频)从JSP上传到Google云存储

转载 作者:太空宇宙 更新时间:2023-11-04 11:57:39 26 4
gpt4 key购买 nike

我有 Google App Engine (JAVA) 服务器和 Google 云存储。在我的服务器中有一个带有上传文件表单的JSP文件(用户从他的计算机中选择文件)。

我需要做的是将文件发送到某个servlet,然后servlet会将文件上传到谷歌云存储。我无法直接从 JSP 页面上传文件。我需要这个操作来自服务器端的 servlet。我尝试了很多东西,我不知道该粘贴到这里。我可以将文件上传到谷歌云存储(GCS),但GCS不知道文件的类型,因此无法打开它。我在这里使用了这个例子:https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-storage

如何正确编写 servlet 和 jsp?

最佳答案

这是我的文件上传代码。

public boolean uploadFile(String filePath, byte[] file) {
try {
setDefaultStorageCredentials();
storage.create(BlobInfo.newBuilder(bucketName, filePath).build(),
new ByteArrayInputStream(file));
return true;
} catch (Exception e) {
return false;
}
}

只要文件名包含文件扩展名,您就不会遇到任何问题。这是我的实现类:

import com.google.auth.Credentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.storage.Blob;

import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
* Uploads a file to Google Cloud Storage to the bucket specified in the
* BUCKET_NAME environment variable, appending a timestamp to end of the
* uploaded filename.
*
* @throws IOException
* @throws FileNotFoundException
*/
@SuppressWarnings("static-access")
public class GoogleCloudStorage {

Variables variables = new Variables();

public GoogleCloudStorage() {
setDefaultStorageCredentials();
}

private static Storage storage = null;
private static Credentials credentials = null;
//Project Id can be obtained from your GCP console dashboard.
private static String projectId = "Your project Id";

//Create the bucket using the REST API or manually using the Google Cloud Storage Browser Interface.
private static String bucketName = "Your bucket name";

//Following 4 parameters can be obtained from the Private Key file.
//Client Id will usually be a numeric string
private static String clientId = "Your Client Id From the Key File.";

//Client Email Id is the email Id generated when you create the service account. This will be in the format of: *.iam.gserviceaccount.com
private static String clientEmailId = "Your Client Email Id From the Key File.";

//Private key can be obtained from the key file. This will be a very long string within the file. Paste the entire string here.
private static String privateKey = "Your Private Key From the Key File.";

//Private Key Id can be obtained from the key file. This is ususally a numeric string.
private static String privateKeyId = "Your Client Id From the Key File.";

/**
* This method sets the storage credentials for the default storage object.
*/
private void setDefaultStorageCredentials() {
try {
credentials = ServiceAccountCredentials.fromPkcs8(clientId, clientEmailId, privateKey, privateKeyId, null);
storage = StorageOptions.newBuilder()
.setCredentials(credentials)
.setProjectId(projectId).build().getService();

} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Uploads a given file to Google Cloud Storage.
*
* @param filePath The desired file path for the file to be uploaded. File path should be absolute path and should include folders, sub-folders, and file name
* @param file The file to be uploaded in byte array format
* @return true if the file has been successfully uploaded; false otherwise
*/
public boolean uploadFile(String filePath, byte[] file) {
try {
setDefaultStorageCredentials();
storage.create(BlobInfo.newBuilder(bucketName, filePath).build(),
new ByteArrayInputStream(file));
return true;
} catch (Exception e) {
return false;
}
}

/**
* Downloads a given file from Google Cloud Storage.
*
* @param filePath The desired file path for the file to be downloaded. File path should be absolute path and should include folders, sub-folders, and file name
* @return the downloaded file in byte array format
*/
public byte[] downloadFile(String filePath) throws FileNotFoundException, IOException {
setDefaultStorageCredentials();
return storage.get(bucketName).get(filePath).getContent();
}

/**
* Generates a temporary link to a file in Google Cloud Storage.
* This will allow temporary access to the file without actually exposing the file.
* Users accessing this link need not sign in using any credentials.
* <p>
* After the expiry time, this link will be expired and general public cannot access the file.
*
* @param filePath The desired file path for the file to be uploaded. File path should be absolute path and should include folders, sub-folders, and file name
* @return String containing the signed url for the file specified.
*/
public String getTemporaryFileLink(String filePath) throws Exception{
setDefaultStorageCredentials();
Blob blob = storage.get(bucketName).get(filePath);
String blobName = blob.getName();
URL signedUrl = storage.signUrl(BlobInfo.newBuilder(bucketName, blobName).build(), 5,TimeUnit.MINUTES);
return signedUrl.toExternalForm();
}

/**
* Deletes a given file from Google Cloud Storage.
*
* @param filePath The desired file path for the file to be deleted. File path should be absolute path and should include folders, sub-folders, and file name
* @return true if the file has been successfully deleted; false otherwise
*/
public boolean deleteFile(String filePath){
setDefaultStorageCredentials();
return storage.delete(storage.get(bucketName).get(filePath).getBlobId());
}

}

引用:Google Cloud Storage Wrapper

关于java - 通过Google应用程序引擎(JAVA)将文件(图像或视频)从JSP上传到Google云存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41208468/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com