在Grails中上传有什么问题?

时间:2014-09-04 14:52:27

标签: grails

在我的申请中,我需要上传照片。我希望用户首先创建相册文件夹,然后在该相册上进行上传后,上传照片。我的相册有:ID,album_name,用户名和我的照片:ID,album_ID,photo_name,type

专辑控制器:

 import java.io.File
import grails.plugin.springsecurity.annotation.Secured
import java.text.SimpleDateFormat

@Secured(['ROLE_USER','ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])

class AlbumController {

    def springSecurityService

    def index() { }

    def create(){
        def user = User.get(springSecurityService.currentUser.id)

        def album = new Album()
        album.a_name = params.name
        album.user = user
        album.save(failOnError:true)

        def photo = new Photo()
        def uploadedFile = request.getFile('myFile')
        def name = System.currentTimeMillis()

        if (uploadedFile.empty) {
            flash.message = 'file cannot be empty'
            redirect (action:'index')
            return
        }

        photo.type = uploadedFile.contentType
        photo.p_name = name
        photo.album = album

        uploadedFile.transferTo(new File("../test101111/web-app/album/" + user.username + "/"+ photo.getP_name() + ".jpg"))
        //response.sendError(200, 'Done')

        photo.save(failOnError:true)
        redirect (action:'index')

    }

}

照片控制器:

import java.io.File
import grails.plugin.springsecurity.annotation.Secured
import java.text.SimpleDateFormat

@Secured(['ROLE_USER','ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])

class PhotoController {
    def springSecurityService

    def index() { }
    def create(){
        def photo = new Photo()
        def uploadedFile = request.getFile('myFile')
        def name = System.currentTimeMillis()

        if (uploadedFile.empty) {
            flash.message = 'file cannot be empty'
            redirect (action:'index')
            return
        }
        photo.type = uploadedFile.contentType
        photo.p_name = name
        photo.album = params.albumId
        uploadedFile.transferTo(new File("../test101111/web-app/album/" + photo.getP_name() + ".jpg"))
        //response.sendError(200, 'Done')
        photo.save(failOnError:true)
        redirect (action:'index')
    }
}

照片视图:

<%@ page contentType="text/html;charset=UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="layout" content="main" />
<title>Photo</title>
<script
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $("span.button").click(function() {
            $("form.forma_album").toggle();
        });
    });
</script>

</head>
<body>
    <div class="body">
        <div class="album_title">
            <span class="title1">Fotografije</span>
            <div class="new_album">
                <img class="plus" src="${resource(dir: 'images', file: 'plus.png')}" />
                <span class="button">Dodaj novu sliku</span>
            </div>
        </div>
        <hr class="usual">
        <g:uploadForm action="create" class="forma_album">
            <input type="file" name="myFile" />
            <input type="submit" />
        </g:uploadForm>
    </div>
</body>
</html>

专辑视图:

<%@ page contentType="text/html;charset=UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="layout" content="main" />
<title>Album</title>
<script
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $("span.button").click(function() {
            $("form.forma_album").toggle();
        });
    });
</script>
</head>
<body>
    <div class="body">

        <div class="album_title">
            <span class="title1">Foto albumi</span>
            <div class="new_album">
                <img class="plus" src="${resource(dir: 'images', file: 'plus.png')}" />
                <span class="button">Kreiraj novi album</span>
            </div>
        </div>
        <hr class="usual">
        <g:form action="create" class="forma_album">
            <g:textArea class="album_name" name="name" placeholder="Ime albuma"></g:textArea>
            <g:submitButton class="submit2" name="Dodaj" />
        </g:form>
        <g:each in="${albums}" var="album">
            <g:link controller="photo" action="index" class="contact"
                params="[albumId: album.id]">
                ${album.a_name}
            </g:link>
        </g:each>
        <%--<g:uploadForm action="create" class="forma_album">
<g:textArea class="album_name" name="name" placeholder="Ime albuma"></g:textArea>
<input type="file" name="myFile" />
<input type="submit" />
</g:uploadForm>

--%>
        <div>
            <%--<g:each in="${albums}" var="album" class="picture">
<div class="picture">
<img src="${resource(dir: 'images', file: 'picture.png')}" />
<div class="album_name">
${album.a_name}
</div>
</div>
</g:each>
--%>
        </div>
    </div>


</body>
</html>

我的错误是:没有方法签名:org.springframework.security.web.servletapi.HttpServlet3RequestFactory $ Servlet3SecurityContextHolderAwareRequestWrapper.getFile()适用于参数类型:(java.lang.String)值:[myFile]可能的解决方案: getXML(),getPart(java.lang.String),getAt(java.lang.String),getAt(java.lang.String),getLocale(),getJSON()

有什么问题?抱歉我的英文,谢谢你的回答;)

2 个答案:

答案 0 :(得分:0)

您可以在grails.servlet.version = "2.5"BuildConfig.groovy

中设置application.properties

使用request.getPart(String)代替request.getFile(String)

请参阅JavaDoc on Part interface

答案 1 :(得分:0)

请你打印参数并将它们粘贴在这里。 或者,您可以尝试使用request.getInputStream()。