File upload 基本文件上载失败

File upload 基本文件上载失败,file-upload,playframework,File Upload,Playframework,我无法在服务器上执行任何上载操作。与玩游戏可以做的五件很酷的事情中的一件相反,以下几点似乎不起作用: public static void doSingleFileUpload( Long id, @Required java.io.File upload, String description, String title ) { Score score = Score.findById(id); try

我无法在服务器上执行任何上载操作。与玩游戏可以做的五件很酷的事情中的一件相反,以下几点似乎不起作用:

    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>
}

视图:

现在的问题是:除了第一个应用程序有更多的参数之外,应用程序之间的区别是什么

谢谢大家迄今为止的帮助

您好,
jasper

不确定您的代码为什么不起作用,但我通过使用Blob而不是File获得了类似的效果

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}

它编译吗?您正在将java.io.File对象传递给doFileUpload,其第一个参数是InputStream。

您使用的是什么版本的Play,doFileUpload的代码是什么样子的。如果是公共静态,那么您的代码将无法工作。谢谢您的回答!我已经用你的答案编辑了我的帖子。。。它不是一个公共方法,但这并不能解释上传的空值,或者是吗?这个例子太复杂了,我在看答案的同时编写代码,因此用不同版本的应用程序编辑我的文章。我将用一个非常简单的文件上传应用程序编辑我的第一篇文章。谢谢你的回答。希望很快收到你的来信!谢谢你的帮助,谢谢!但是在开发过程的这个阶段,我不能对数据库结构进行任何更改。我不确定是否也要将所有二进制文件存储在数据库中。。。但我真的很感谢你的努力!
#{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}
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}