1
我需要使用谷歌雲端點將文件上傳到谷歌雲端存儲並打印回網址到文件。通過谷歌雲端上傳文件端點到谷歌雲端通過Android客戶端存儲
我不想運行一個獨立的servlet處理文件上傳。
服務器的代碼如下所示:
import java.io.File;
public void saveFile(File upload,User auth) throws IOException {
if (auth!=null){
String bucketName = "app-id.appspot.com";
GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder()
.initialRetryDelayMillis(10)
.retryMaxAttempts(10)
.totalRetryPeriodMillis(15000)
.build());
String sname = upload.getName();
String extension = sname.substring(sname.lastIndexOf('.'),sname.length());
String sctype = URLConnection.guessContentTypeFromName(upload.getName());
String filename;
filename = String.valueOf(Calendar.getInstance().getTimeInMillis()) + extension;
GcsFilename gcsfileName = new GcsFilename(bucketName, filename);
GcsFileOptions options = new GcsFileOptions.Builder()
.acl("public-read").mimeType(sctype).build();
GcsOutputChannel outputChannel =
gcsService.createOrReplace(gcsfileName, options);
InputStream stream = new FileInputStream(upload);
copy(stream, Channels.newOutputStream(outputChannel));
}
}
private static final int BUFFER_SIZE = 2 * 1024 * 1024;
private void copy(InputStream input, OutputStream output) throws IOException {
try {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = input.read(buffer);
while (bytesRead != -1) {
output.write(buffer, 0, bytesRead);
bytesRead = input.read(buffer);
}
} finally {
input.close();
output.close();
}
}
但是,當我重新生成項目,進口java.io.File
被轉換成com.backend.managerApi.model.File
在生成的客戶端庫。
那麼,有沒有辦法做到這一點,或者我們將只需要運行一個獨立的servlet來處理上傳?