play框架之文件上传

来源:互联网 发布:如何生成短信淘宝链接 编辑:程序博客网 时间:2024/06/02 02:33
<span style="font-size:18px;"><!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body>    <form action="/upload.do" method="POST" enctype="multipart/form-data">        <input type="file" name="myfile">        <input type="submit"/>    </form></body></html></span>

routes路由表 post    /upload.do      controllers.xxx.upload

后台实现:

<span style="font-size:18px;">public static Result upload() throws IOException{MultipartFormData body = request().body().asMultipartFormData();  FilePart filepart = body.getFile("myfile");  if (filepart != null) {    String fileName = filepart.getFilename();    String contentType = filepart.getContentType();     File file = filepart.getFile();//获取到默认上传保存的完整文件路径,这只是个临时文件    BufferedInputStream is=new BufferedInputStream(new FileInputStream(file));    File newFile=new File("e://upload/"+filepart.getFilename());//要保存的文件路径    BufferedOutputStream os=new BufferedOutputStream(new FileOutputStream(newFile));    byte[] b = new byte[1024];     while(is.read(b)!=-1){    os.write(b);    }    is.close();    os.close();    return ok("File uploaded");  } else {    flash("error", "Missing file");        return ok("aa");      }}</span>


0 0
原创粉丝点击