2011-04-14 117 views
1

我無法在服務器上執行任何上傳操作。相反,「五個很酷的事情,你可以玩做一」,下面似乎不工作:基本文件上傳失敗

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看起來是這樣的:

@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(); 

} 

有了這樣的觀點:

<!-- ... --> 
    <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爲空?我發現了類似的問題here。但是當我在POST之後查看標題時,除了playsession鍵外,其他信息很少...

我在做什麼錯了?

我使用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 2011-04-14 16:47:00

+0

感謝您的回答!我已經編輯了我的帖子,回答了你的答案......它不是一個公共方法,但是它不能解釋'upload'的'null'值,還是這樣做? – Jasper 2011-04-17 19:53:48

回答

1

不知道爲什麼你的代碼不能正常工作,但我得到了類似的東西用的,而不是文件的Blob工作。

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 2011-04-20 10:02:11

2

這是否編譯?您將java.io.File對象傳遞給doFileUpload,其第一個參數是InputStream

+0

這個例子太複雜了,我正在編寫代碼,因爲我在查看答案,因此使用不同版本的應用程序編輯我的帖子。我將用一個非常簡單的文件上傳應用程序編輯我的第一篇文章。感謝您的回答。希望早日收到你的消息! – Jasper 2011-04-20 09:41:29