【发布时间】:2015-08-28 22:49:41
【问题描述】:
我需要使用谷歌云端点将文件上传到谷歌云存储并打印回文件的网址。
我不想运行一个独立的 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 来处理上传?
【问题讨论】:
标签: java android google-app-engine google-cloud-storage google-cloud-endpoints