如何将 AppEngineFile 转换为字节数组?
我有像这样从 blobkey 创建的文件
AppEngineFile file = fileService.getBlobFile(new BlobKey("blob key"));
我已经尝试过这样的事情
FileService fileService = FileServiceFactory.getFileService();
// Create a new Blob file with mime-type "text/plain"
AppEngineFile file = fileService.getBlobFile(new BlobKey(video.getBlobkey()));
BlobstoreInputStream b = new BlobstoreInputStream(new BlobKey(video.getBlobkey()));
RandomAccessFile f = new RandomAccessFile(file.getFullPath(), "r");
byte[] pixels = b.read(); //doesn't work
这个想法是通过 POST 请求发送这个字节数组。
这是我必须构建请求的代码:
String attachmentName = "test";
String attachmentFileName = "test.mp4";
String crlf = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
HttpURLConnection httpUrlConnection = null;
URL url = new URL("http://example.com/server.cgi");
httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setDoOutput(true);
DataOutputStream request = new DataOutputStream(httpUrlConnection.getOutputStream());
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" + attachmentName + "\";filename=\"" + attachmentFileName + "\"" + crlf);
request.writeBytes(crlf);
// Get a file service
FileService fileService = FileServiceFactory.getFileService();
// Create a new Blob file with mime-type "text/plain"
AppEngineFile file = fileService.getBlobFile(new BlobKey(video.getBlobkey()));
BlobstoreInputStream b = new BlobstoreInputStream(new BlobKey(video.getBlobkey()));
RandomAccessFile f = new RandomAccessFile(file.getFullPath(), "r");
byte[] pixels = b.read();
request.write(pixels);
request.writeBytes(crlf);
request.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
request.flush();
request.close();
InputStream responseStream = new BufferedInputStream(httpUrlConnection.getInputStream());
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
String line = "";
StringBuilder stringBuilder = new StringBuilder();
while ((line = responseStreamReader.readLine()) != null)
{
stringBuilder.append(line).append("\n");
}
responseStreamReader.close();
String response = stringBuilder.toString();
System.out.println(response);