作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何将 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);
最佳答案
你可以简单地这样做:
FileReadChannel ch = fileservice.openReadChannel(file, true);
byte[] data = getBytes(Channels.newInputStream(ch));
并使用这个方便的方法(可在其他地方使用):
public static byte[] getBytes(InputStream is) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int len;
byte[] data = new byte[100000]; // adapt buffer size to your needs
while ((len = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, len);
}
buffer.flush();
return buffer.toByteArray();
}
关于java - AppEngineFile 到 byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14966757/
如何将 AppEngineFile 转换为字节数组? 我的文件是这样从 blobkey 创建的 AppEngineFile file = fileService.getBlobFile(new Blo
我的应用程序需要在 Google App Engine 中提取 rar 文件。我可以使用 this library 提取 rar 文件但它只支持java.io.File,不支持AppEngineFil
我是一名优秀的程序员,十分优秀!