2013-02-07 2 views
2

Google 애플리케이션 엔진 페이지에서 "Blobstore (Experimental)에 파일 쓰기"를 이미 보았습니다.프로그래밍 방식으로 자바를 사용하여 Google 애플리케이션 엔진 blobstore에 이미지 업로드

// Get a file service 
    FileService fileService = FileServiceFactory.getFileService(); 

    // Create a new Blob file with mime-type "text/plain" 
    AppEngineFile file = fileService.createNewBlobFile("text/plain"); 

    // Open a channel to write to it 
    boolean lock = false; 
    FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock); 

    // Different standard Java ways of writing to the channel 
    // are possible. Here we use a PrintWriter: 
    **PrintWriter** out = new PrintWriter(Channels.newWriter(writeChannel, "UTF8")); 
    out.println("The woods are lovely dark and deep."); 
    out.println("But I have promises to keep."); 

    // Close without finalizing and save the file path for writing later 
    out.close(); 
    String path = file.getFullPath(); 

    // Write more to the file in a separate request: 
    file = new AppEngineFile(path); 

    // This time lock because we intend to finalize 
    lock = true; 
    writeChannel = fileService.openWriteChannel(file, lock); 

    // This time we write to the channel directly 
    writeChannel.write(ByteBuffer.wrap 
      ("And miles to go before I sleep.".getBytes())); 

    // Now finalize 
    writeChannel.closeFinally(); 

    // Later, read from the file using the file API 
    lock = false; // Let other people read at the same time 
    FileReadChannel readChannel = fileService.openReadChannel(file, false); 

    // Again, different standard Java ways of reading from the channel. 
    BufferedReader reader = 
      new BufferedReader(Channels.newReader(readChannel, "UTF8")); 
     String line = reader.readLine(); 
    // line = "The woods are lovely dark and deep." 

    readChannel.close(); 

    // Now read from the file using the Blobstore API 
    BlobKey blobKey = fileService.getBlobKey(file); 
    BlobstoreService blobStoreService = BlobstoreServiceFactory.getBlobstoreService(); 
    String segment = new String(blobStoreService.fetchData(blobKey, 30, 40)); 

불행하게도,이 단지 텍스트 파일입니다 :

이 내가 가진 것입니다. PrintWriterImageWriter으로 변경해야하지만 Google 앱 엔진에서는 ImageWriter이 지원되지 않는다고 가정합니다. 어떻게해야합니까?

답변

3

당신은 단순히 바이너리 데이터를 Blob 저장소에 쓸 수 있습니다

byte[] yourBinaryData = // get your data from request 
writeChannel.write(ByteBuffer.wrap(yourBinaryData)); 
+0

감사합니다. 이제, 나는 그림을 저장할 수 있고 또한 블롭 스토어에서 얻을 수 있습니다. 그러나 그림 (블롭 스토어에서)은 분명하지 않습니다. 좋은 해결책이 없습니다. (미안 해요, 영어 글쓰기에 능숙하지 않습니다 :() 입력 스트림 유형에 그림을 저장하는 다른 방법이 있습니까? –

+0

Blobstore는 데이터 내용을 변경하지 않습니다. 어딘가에 –

+0

예 바이트 변수의 크기가 문제가되었습니다. 그래서 inputstream의 크기를 가져 와서 바이트 변수의 크기를 inputstream 변수의 크기로 변경합니다. 그래서 도움을 주신 BLOBSTORE에 그림을 저장할 수 있습니다. –

3

당신은이 작업을 수행해야합니다 귀하의 답변에 대한

public static BlobKey toBlobstore(Blob imageData) throws FileNotFoundException,  FinalizationException, LockException, IOException { 
    if (null == imageData) 
     return null; 

    // Get a file service 
    FileService fileService = FileServiceFactory.getFileService(); 

    // Create a new Blob file with mime-type "image/png" 
    AppEngineFile file = fileService.createNewBlobFile("image/jpeg");// png 

    // Open a channel to write to it 
    boolean lock = true; 
    FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock); 

    // This time we write to the channel directly 
    writeChannel.write(ByteBuffer.wrap 
     (imageData.getBytes())); 

    // Now finalize 
    writeChannel.closeFinally(); 
    return fileService.getBlobKey(file); 
}