본문 바로가기
Programming/Google

[GAE] 파일 업로드

by NAMP 2015. 11. 30.

[GAE] 파일 업로드




eclipse MARS 에서 개발하고 있습니다. 



POM.XML 파일 수정

 <!-- MultipartHttpServletRequset -->

<dependency>

   <groupId>commons-io</groupId>

   <artifactId>commons-io</artifactId>

   <version>2.0.1</version>

</dependency> 

 

<dependency>

   <groupId>commons-fileupload</groupId>

   <artifactId>commons-fileupload</artifactId>

   <version>1.2.2</version>

</dependency>

<dependency> 

   <groupId>org.apache.commons</groupId>

   <artifactId>commons-io</artifactId> 

   <version>1.3.2</version> 

</dependency>



JSP 에서 Form으로 전송

<form Method="POST" id="frm" name="frm" enctype="multipart/form-data" 

action="/subtitle/addfile">

<input type="file" name="file"> 

<br /><br /> 

<button type="submit" name="upload" value="upload">upload</button>

</form>



Controller 에서 처리

@RequestMapping(value="/addfile")

public String addfile(ModelMap model, HttpServletRequest req) throws FileUploadException, IOException {

System.out.println("/addfile called");


// Get the image representation

   ServletFileUpload upload = new ServletFileUpload();

   FileItemIterator iter = upload.getItemIterator(req);

   FileItemStream item = iter.next();

   InputStream stream = item.openStream();

   

   // construct our entity objects

   byte[] bytes = IOUtils.toByteArray(stream);

   Blob imageBlob = new Blob(bytes);

   System.out.println("BLOB: " + imageBlob.toString());

   

   String str = new String(bytes,0,bytes.length);

   String[] lines = str.split("\n");

   int i = 1;

   

   SyncInfo syncInfo = new SyncInfo();

   for (String line : lines) {

//System.out.println(i++ + " : " + line);

syncInfo = getSyncInfo(line);

}

       

return prefix + "list";

}




댓글