如何保存到Grails中的文件系统目录 [英] How to save to a file system directory in Grails
问题描述
我正在将它保存在我的数据库中,而不是保存在文件系统目录中,并允许其他用户下载它。在我的文件系统目录中。这是我的代码:
pre code $ class $ Document b $ b字符串文件
字节filedata
Date uploadDate = new Date()
static constraints = {
filename(blank:false,nullable:false)
filedata(blank:true,nullable:true,maxSize:1073741824 )
}
}
和我上传文件的控制器是:
类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(){
}
$ b $ def upload(){
def file = request.getFile('file')
if(file.isEmpty())
{
flash.message =文件不能为空
}
else
{
def documentInstance = new Document()
documentInstance.filename = file.getOriginalFilename()
documentInstance.filedata = file.getBytes()
documentInstance.save()
}
重定向(action:'list')
}
}
我认为你可以做一个类似于下面的功能:
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.
这篇关于如何保存到Grails中的文件系统目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!