我试图通过JSP将文件上传到我的GCS没有成功。
使用这个html:
<form enctype="multipart/form-data" action="/gcs/uploud/" method="POST">
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
控制器:
@Controller
@RequestMapping("pages/gcs/*")
public class GoogleStorageController {
public static final boolean SERVE_USING_BLOBSTORE_API = false;
private final GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder()
.initialRetryDelayMillis(10)
.retryMaxAttempts(10)
.totalRetryPeriodMillis(15000)
.build());
private static final int BUFFER_SIZE = 2 * 1024 * 1024;
private static String BucketName = "XXXXXX";
@RequestMapping(value ="*/*",method = RequestMethod.POST)
public @ResponseBody String home(HttpServletRequest req, HttpServletResponse resp) {
GcsFileOptions instance = GcsFileOptions.getDefaultInstance();
GcsFilename fileName = getFileName(req);
GcsOutputChannel outputChannel;
try {
outputChannel = gcsService.createOrReplace(fileName, instance);
copy(req.getInputStream(), Channels.newOutputStream(outputChannel));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "successPage";
}
private GcsFilename getFileName(HttpServletRequest req) {
return new GcsFilename(BucketName, "media");
}
/**
* Transfer the data from the inputStream to the outputStream. Then close both streams.
*/
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();
}
}
}
我的GCS中有文件,但标题不正确。
你知道吗? 我应该如何处理HTTP标头请求? 我该如何定义内容类型字段?答案 0 :(得分:0)
您没有在createOrReplace()
中设置任何GcsFileOptions,因此我希望您上传的对象具有完全默认值。尝试这样的事情:
GcsFileOptions.Builder options = new GcsFileOptions.Builder();
options.mimeType("text/plain-or-whatever")
outputChannel = gcsService.createOrReplace(fileName, options.build(), instance);