ligson521
ligson521
  • 发布:2015-03-24 16:25
  • 更新:2015-03-24 16:33
  • 阅读:1382

文件上传 服务端用java grails 框架 无法解析文件头

分类:HTML5+
2015-03-24 16:25 负责人:无 分享
已邀请:
ligson521

ligson521 (作者)

7.1.9 Uploading Files

Programmatic File Uploads
Grails supports file uploads using Spring's MultipartHttpServletRequest interface. The first step for file uploading is to create a multipart form like this:

Upload Form: <br />
<g:uploadForm action="upload">
<input type="file" name="myFile" />
<input type="submit" />
</g:uploadForm>
The uploadForm tag conveniently adds the enctype="multipart/form-data" attribute to the standard <g:form> tag.

There are then a number of ways to handle the file upload. One is to work with the Spring MultipartFile instance directly:

def upload() {
def f = request.getFile('myFile')
if (f.empty) {
flash.message = 'file cannot be empty'
render(view: 'uploadForm')
return
}
f.transferTo(new File('/some/local/dir/myfile.txt'))
response.sendError(200, 'Done')
}

This is convenient for doing transfers to other destinations and manipulating the file directly as you can obtain an InputStream and so on with the MultipartFile interface.

该问题目前已经被锁定, 无法添加新回复