2011-04-14 2 views
1

서버에서 업로드 작업을 수행 할 수 없습니다. , 다음이 보이지 않는다 "당신이 플레이 할 수있는 다섯 가지 멋진 것들 중 하나"반대로 작동합니다 :기본 파일 업로드가 실패합니다.

:이보기와

@Check("registered") 
private static String doFileUpload(InputStream is, Score score) throws IOException { 
    //get Score from db 
    //get dir if present 
    java.io.File dir = new java.io.File("/public/uploads/" + play.templates.JavaExtensions.slugify(score.title)); 
    //if not, create 
    if (!dir.exists()) { 
     dir.mkdirs(); //create new dir if not present 
    } 
    //create file on server 
    java.io.File newfile = new java.io.File(dir, "testfile.txt"); 
    OutputStream os = new FileOutputStream(newfile); 
    IOUtils.copy(is, os); 
    return newfile.getAbsolutePath(); 

} 

:

public static void doSingleFileUpload(
      Long id, @Required java.io.File upload, String description, String title 
      ) { 
     Score score = Score.findById(id); 

     try { 
      score.files.add(new File(doFileUpload(new FileInputStream(upload), score), title));//PLAY crashes here, with a nullpointer exception on the upload parameter 
     } catch (IOException ex) { 
      //TODO: do something nice 
     } 
     score.save(); 
    } 

doFileUpload

은 다음과 같습니다
<!-- ... --> 
    <form action="@{ScoreController.doSingleFileUpload()}" method="POST" enctype="multipart/form-data"> 
    <input type="hidden" name="id" value="${score.id}" /> 
    <input type="file" id="upload" name="upload" /> 
    <input type="text" name="description" /> 
    <input type="hidden" name="title" value="${score.title}" /> 
    <input type="submit" value="submit" /> 
</form> 

upload은 null입니까? 비슷한 문제가 발생했습니다. here. 하지만 POST 후 헤더를 보면, 거기에 정보가 거의 없다는 것입니다.

내가 뭘 잘못하고 있니?

FF4로 테스트 중이며 Play 1.1.1을 사용 중입니다.


편집 : 이 샘플 응용 프로그램은 1.1.1 및 1.2에서 모두 작동합니다.

컨트롤러 :

public class Application extends Controller { 

public static void index() { 
    File dir = new File(Play.applicationPath+File.separator+"public"+File.separator+"uploads"); 
    if (!dir.exists() && dir.isDirectory()) { 
     renderText("something went wrong"); 
    } else { 
     String[] files = dir.list(); 
     if (files != null) { 
      render(dir); 
     } else { 
      render(); 
     } 
    } 
} 

public static void upload(File upload) throws FileNotFoundException, IOException { 
    File dir = new File(Play.applicationPath+File.separator+"public"+File.separator+"uploads"); 
    if (!dir.exists()) { 
     dir.mkdirs(); 
    } 
    File newfile = new File(dir, upload.getName()); 
    FileInputStream fis = new FileInputStream(upload); 
    FileOutputStream fos = new FileOutputStream(newfile); 
    IOUtils.copy(fis, fos); 
    index(); 
} 

}

보기 :

#{extends 'main.html' /} 
#{set title:'Home' /} 

#{form @Application.upload(), enctype:'multipart/form-data'} 
<input type="file" name="upload" /> 
<input type="submit" /> 
#{/form} 
#{if (dir.list()!=null)} 
<ul> 
    #{list items:dir.list(), as:'file'} 
    <li><a href="public/uploads/${file}">${file}</a></li> 
    #{/list} 
</ul> 
#{/if} 

이제 질문은 다음과 같습니다 응용 프로그램 사이의 차이는 첫 번째가 더 많이 가지고 있다는 사실을 제외하고는 무엇입니까 매개 변수 ...

모두에게 도움을 주셔서 감사합니다!

인사, 재스퍼

+0

어떤 Play 버전을 사용하고 있으며 doFileUpload의 코드는 무엇입니까? 공개 정적 인 경우 코드가 작동하지 않습니다. – Codemwnci

+0

답변 해 주셔서 감사합니다. 귀하의 답변에 대한 답변으로 내 게시물을 편집했습니다 ... 공개 방법은 아니지만 '업로드'에서 'null'값을 설명하지 않습니까? – Jasper

답변

1

하지 코드가 작동하지 않는 이유를 모르겠지만, 내가 대신 파일의 물방울을 사용하여 작업 비슷한 얻었다.

public static void update(long userId, 
          String aboutMyself, 
          String location, 
          Blob profilePicBlob) { 

    UserProfile userProfile = UserProfile.find("select distinct upr from UserProfile upr where upr.user.id = ?", userId).first(); 
    if(profilePicBlob != null) { 
     //TODO: Delete the old profile pic 
     userProfile.profilePic = new Pic(profilePicBlob).save(); 
    } 
    userProfile.aboutMyself = aboutMyself; 
    userProfile.location = location; 
    userProfile.save(); 

    show(userId); 
} 

@Entity 
public class Pic extends Model { 
    public Blob image; 

    public Pic(Blob image) { 
     this.image = image; 
    } 
} 

#{form @UserProfileC.update(userProfile.user.id), enctype:'multipart/form-data'} 
    <div> 
     <div>About Myself:</div> 
     <textarea rows="10" cols="80" name="aboutMyself">${userProfile.aboutMyself</textarea> 
    </div> 
    <div> 
     <div>Location</div> 
     <input type="text" name="location" value="${userProfile.location}"/> 
</div> 
    <div>     
     <input type="file" name="profilePicBlob" /> 
     <label>${userProfile.profilePic == null ? 'Upload' : 'Change'} profile pic</label> 
    </div> 
    <div> 
     <input type="submit" value="Submit" id="postComment" /> 
    </div> 
#{/form} 
+0

도움 주셔서 감사합니다, 고맙습니다! 그러나 개발 프로세스의이 단계에서는 데이터베이스 구조를 변경할 수 없습니다. 데이터베이스에 모든 이진 파일을 저장하고 싶은지 확실하지 않습니다 ...하지만 정말 고맙습니다! – Jasper

2

컴파일됩니까? java.io.File 객체를 doFileUpload에 전달하고 있습니다. 첫 번째 매개 변수는 InputStream입니다.

+0

이 예제는 너무 복잡합니다. 답변을보고 있으므로 코드를 작성하므로 다른 버전의 응용 프로그램으로 게시물을 편집 할 수 있습니다. 아주 간단한 파일 업로드 응용 프로그램으로 첫 번째 게시물을 편집하겠습니다. 귀하의 답변에 감사드립니다. 곧 당신에게서 소식을 듣고 싶습니다! – Jasper

관련 문제