本文由 发布,转载请注明出处,如有问题请联系我们! 发布时间: 2021-08-01input上传文件获取内容-文件或目录损坏且无法读取方法
加载中近几天忙着做题,但又怕忘记了春靴,就花一点时间瞎折腾,加重印像。
spring boot的图片上传和spring mvc基本一致,只必须留意一些配备。
自然环境:spring boot v 1 . 5 . 1 . release JDK 1.7 髓鞘。
1).引进迷迭香叶适用网页跳转。
org.springframework.boot
迷迭香叶
123452).在 src/main/resources 文件目录下新创建 static 文件目录和 templates 文件目录。 static储放静态数据文档,例如 css,js,image… templates 储放静态网页。先往templates 中创建一个 uploadimg.html2).在src/main/resources文件目录下建立新的静态数据文件目录和模版文件目录。静态数据储存静态数据文档,如css,js,image…模版储存静态网页。最先在模版中建立新的uploadimg.html。
uploadimg.htmluploadimg.html
–>
画
12345678910111213141516171819203)在开关中撰写2个方式,一个方式自动跳转到文件上传的网页页面,另一个方式解决文件上传。
//自动跳转到文件上传网页页面。
@RequestMapping(value=”/gouploadimg”, method = RequestMethod.GET)@ request mapping(value =/goupalodigm),method = RequestMethod。GET)
公共性字符串数组GoupLoadimg(){ 0
//自动跳转到 templates 文件目录下的 uploadimg.html//自动跳转到模版文件目录中的uploadimg.html。
return “uploadimg”;回到“uploadimg”;
}
//解决上传文件。
@RequestMapping(value=”/testuploadimg”, method = RequestMethod.POST)@ request mapping(value =/testuploadimg),方式= RequestMethod。开机自检)
public @ response body String uploadImg(@ request param(" file ")多一部分文档文档,
HttpServletRequest要求){ 0
string ContentType = file . GetContentType();
string fileName = file . getoriginalfilename();
/* system . out . println(" fileName –> " fileName ");
system . out . println(" getcontent type–> " content type);*/
String filePath = request.getSession().getServletContext().getRealPath(“imgupload/”);string file path = request . GetSession()。getServletContext()。getRealPath(" imgupload/";
试着{
fileutil . uploadfile(file . getbytes(),filePath,fileName);
}捕捉(除外e){ 0
// TODO:解决出现异常
}
//回到json。
return “uploadimg success”;回到“提交取得成功”;
}
1234567891011121314151617181920212223244)在上面,我写了java工具FileUtil的uploadFile方式中上传文件的完成。
公共性静态数据失效文件上传(字节数[]文档,字符串数组文件路径,字符串数组文件夹名称)引起出现异常{
文档总体目标文档=新文档(文件路径);
if(!target file . exists()){ 0
target FIle . mkdirs();
}
文档輸出流輸出=新文档輸出流(文件路径 文件夹名称);
out.write(文档);
out . flush();
out . close();
}
123456789105).在电脑浏览器键入 :http://localhost:8080/gouploadimg 检测5).在网页中键入:http://localhost:8080/gouploadimg开展检测。
文件上传后:在使用的 src/main/webapp/imgupload 文件目录下到应用软件的src/main/webapp/imgupload文件目录下。
6).假如提交的文档超过1M,将汇报文件过大的不正确。在application.properties中设定文件上传的主要参数。spring . http . multipart . maxfilesize = 100 Mb
spring . http . multipart . MaxRequestSize = 100 Mb