Jas*_*son 3 grails filenotfoundexception image-upload
我正在为Grails中的Web应用程序实现文件上传功能.这包括调整现有代码以允许多个文件扩展名.在代码中,我实现了一个布尔值来验证文件路径是否存在,但我仍然得到一个FileNotFoundException,/hubbub/images/testcommand/photo.gif (No such file or directory)
我的上传代码是
def rawUpload = {
def mpf = request.getFile("photo")
if (!mpf?.empty && mpf.size < 200*1024){
def type = mpf.contentType
String[] splitType = type.split("/")
boolean exists= new File("/hubbub/images/${params.userId}")
if (exists) {
mpf.transferTo(new File("/hubbub/images/${params.userId}/picture.${splitType[1]}"))
} else {
tempFile = new File("/hubbub/images/${params.userId}").mkdir()
mpf.transferTo(new File("/hubbub/images/${params.userId}/picture.${splitType[1]}"))
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到了异常消息
if (exists) {
mpf.transferTo(new File("/hubbub/images/${params.userId}/picture.${splitType[1]}"))
}
Run Code Online (Sandbox Code Playgroud)
那么,为什么会发生这种错误,因为我只是简单地折叠有效的现有路径以及有效的文件名和扩展名?
为什么你认为File对象的转换Boolean返回文件的存在?
尝试
File dir = new File("/hubbub/images/${params.userId}")
if (!dir.exists()) {
assert dir.mkdirs()
}
mpf.transferTo(new File(dir, "picture.${splitType[1]}"))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1119 次 |
| 最近记录: |