且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何保存到Grails中的文件系统目录

更新时间:2023-11-29 14:17:22

我认为你可以做一个类似于下面的功能:

  boolean upload(MultipartFile uploadFile,String fileUploadDir){
String uploadDir =!fileUploadDir.equals('')?:'C :/ temp'//你定义文件的保存路径
File newFile = new File($ uploadDir / $ {uploadFile.originalFilename}); //你创建目标文件
uploadFile.transferTo(newFile); //传输数据

/ **您需要创建一个独立的域来存储文件的路径或者直接在您的域中存放路径* /

由于你只需要保存文件的路径,你可以添加一个字符串到你的域存储它,或者你可以创建一个独立的域来存储你的文件的数据。您还需要在需要的地方添加try / catch语句。

要检索文件,您需要添加到您的控制器,如下面的代码:

 文件downloadFile =新建文件(yourFileDomain?.pathProperty)//使用您保存在域中的数据获取文件
if (downloadFile){//设置你的响应属性
response.characterEncoding =UTF-8
response.setHeaderContent-disposition,attachment; filename = \$ {yourFileDomain?.fileNameProperty } \//将头文件保存在您的域名中,也可以设置默认文件名
//response.setHeaderContent-disposition,attachment; filename = \myfile。 txt \
response.outputStream<< new FileInputStream(downloadFile)
response.outputStream.flush()
return
}

希望这有帮助,欢迎任何意见。


I am trying to save an uploaded file into the file system directory, and allow other users to download it.

I am currently saving it in my database and not in my file system directory. Here is my code:

class Document {
    String filename
    byte[] filedata           
    Date uploadDate = new Date()

    static constraints = {
        filename(blank: false, nullable:false)
        filedata(blank: true, nullable: true, maxSize:1073741824)
    }
}

and my controller for uploading the file is:

class DocumentController {

    static allowedMethods = [delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = 10
        [documentInstanceList: Document.list(params), documentInstanceTotal: Document.count()]
    }

    def uploadPage() {

    }

    def upload() {
        def file = request.getFile('file')
        if(file.isEmpty())
        {
            flash.message = "File cannot be empty"
        }
        else
        {
            def documentInstance = new Document()
            documentInstance.filename = file.getOriginalFilename()
            documentInstance.filedata = file.getBytes()
            documentInstance.save()    
        }
        redirect (action: 'list')
    }
}

I think you could do a fuction similar to the one below:

boolean upload(MultipartFile uploadFile, String fileUploadDir){
    String uploadDir = !fileUploadDir.equals('') ?: 'C:/temp' //You define the path where the file will be saved
    File newFile = new File("$uploadDir/${uploadFile.originalFilename}"); //You create the destination file
    uploadFile.transferTo(newFile); //Transfer the data

    /**You would need to create an independent Domain where to store the path of the file or have the path directly in your domain*/

}

Since you will only need to save the path of the file you could add a string to your domain to store it or you could create an independent domain to store the data of your file. You will also need to add try/catch statements where needed.

And to retrieve the file you would need to add to your controller something like the next code:

File  downloadFile = new File(yourFileDomain?.pathProperty) //get the file using the data you saved in your domain
if(downloadFile){ //Set your response properties
            response.characterEncoding = "UTF-8"
            response.setHeader "Content-disposition", "attachment; filename=\"${yourFileDomain?.fileNameProperty}\"" //add the header with the filename you saved in your domain you could also set a default filename
            //response.setHeader "Content-disposition", "attachment; filename=\"myfile.txt\""
            response.outputStream << new FileInputStream(downloadFile) 
            response.outputStream.flush()
            return
        }

Hope this helps, any comments are welcome.