1

AppEngineFileをバイトの配列に変換する方法は?

このようにブロブキーからファイルを作成しました

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);
4

2 に答える 2

3

あなたは単にこれを行うことができます:

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();
}
于 2013-02-20T10:08:39.153 に答える
2
AppEngineFile file = ...

int size = (int) fileservice.stat(file).getLength().longValue();

    FileReadChannel ch = fileservice.openReadChannel(file, true);
    ByteBuffer dst = ByteBuffer.allocate(size);
    try {
        int sum=0;
        while (sum<size) {
           int read = ch.read(dst);
           sum+=read;
        }
    } finally {ch.close();}

    bytes byte[] = dst.array();

(これは Java でチャネルから読み取る方法ではありませんが、appengine にバグがあり、正確なバイト数を知る必要があるようです)。

于 2013-02-19T21:00:36.133 に答える