2013-04-24 2 views
0

Play Framework 2.0.4를 사용 중입니다. 다음은 시도한 코드입니다.로컬 시스템의 지정된 위치에 이미지 저장

public static Result save() throws FileNotFoundException { 

    Form<Tenant> tenantForm = form(Tenant.class).bindFromRequest(); 
    Form<Ten> tenForm = form(Ten.class).bindFromRequest(); 
    Long tenantid = tenForm.get().tenant_id; 


    Http.MultipartFormData body = request().body().asMultipartFormData(); 
    Http.MultipartFormData.FilePart picture = body.getFile("logo_url"); 

    if (picture != null) { 
     String fileName = picture.getFilename(); 
     String contentType = picture.getContentType(); 
     File file = picture.getFile(); 
     tenantForm.get().logo_url = file.getPath(); 
     tenantForm.get().save(); 


     return redirect(
      routes.Application.index() 
    ); 
    } else { 
     flash("error", "Missing file"); 
     return redirect(
      routes.Project.ctstenant(0,"name","asc","","",tenantid) 
     ); 
    } 
} 

임시 폴더에 이미지가 저장됩니다. 지정된 폴더에 저장하고 싶습니다. 예제와 함께 감사하겠습니다.

도움 주셔서 감사합니다.

+1

왜 파일을 임시에서 원하는 위치로 이동하지 않으시겠습니까? – Carsten

답변

0

파일을 TEMP 폴더에서 파일 저장 디렉터리로 이동할 수 있습니다. 성공적으로 이동하거나 업로드 된 파일의 이름을 변경하기 전에, fileStoragePath에 정의 된 경로를 사용할 수 있어야합니다

// define file storage path 
public static final String fileStoragePath = "D:\\filestorage\\"; 

// handle form submit action 
public static Result save() { 

    // bind request logic 
    ... 

    if (picture != null) { 
     String fileName = picture.getFilename(); 
     String contentType = picture.getContentType(); 
     File file = picture.getFile(); 

     // log message to console 
     Logger.info("Original file name = " + fileName + 
     " with Content Type " + contentType); 

     // Rename or move the file to the storage directory 
     if (file.renameTo(new File(fileStoragePath + fileName))) { 
     Logger.info("Success moving file to " + file.getAbsolutePath()); 
     } else { 
     Logger.info("Failed moving file on " + file.getAbsolutePath()); 
     } 

     // save your file name or using blob (it is your choice) 
     ... 
    } 
} 

하는 것으로 아래에 업로드 한 파일을 이동하는 방법을 예입니다.

+0

답장을 보내 주셔서 감사합니다. – joan

관련 문제