赞
踩
springmvc web开发tomcat配置上传文件到本地硬盘!近期给公司开发网站时候,想让上传的图片,视频,等信息,存在在本地硬盘里,这样可以避免tomcat服务器的耦合。下面是做了一些配置参数,分享给大家,参考借鉴。
首先给大家控制器里面的代码情况。
public ResponseResult<Proimage> uploadChengjiPeitu(HttpServletRequest request, @RequestParam("chengjiimagefile") MultipartFile peitu) { ResponseResult<Proimage> rr = new ResponseResult<Proimage>(); try { byte[] bytes = null; // www/www/ftpusergrzx/newspeitu/ String uploadDir = "D:\\chengjipeituimage\\"; String fileRrealName = peitu.getOriginalFilename(); long time = System.currentTimeMillis(); String t = String.valueOf(time / 1000L); String fileName = String.valueOf(t) + fileRrealName.substring(fileRrealName.lastIndexOf(".")); String filePath = String.valueOf(uploadDir) + fileName; // 给后端发布人员看见的显示图片的路径地址。这是一个相对地址。不是物理路径。 String xianshisrc = "/chengjipeitu/" + fileName; BufferedOutputStream bos = null; FileOutputStream fos = null; try { bytes = peitu.getBytes(); File temp = new File(filePath); if (!temp.exists()) temp.createNewFile(); fos = new FileOutputStream(temp); bos = new BufferedOutputStream(fos); bos.write(bytes); rr.setState(Integer.valueOf(1)); rr.setMessage("成绩配图上传成功"); Proimage pi = new Proimage(); pi.setImgurl(xianshisrc); rr.setData(pi); return rr; } catch (Exception e) { e.printStackTrace(); rr.setState(Integer.valueOf(0)); rr.setMessage("请重试"); return rr; } finally { if (bos != null) try { bos.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } catch (Exception e) { rr.setState(Integer.valueOf(0)); rr.setMessage("服务器异常请重试"); return rr; } }
如图所示,这是上传接口类,里面的uploadChengjiPeitu(上传成绩配图)的接口方法定义。
里面可以看到,有一个linux系统的本地硬盘文件的路径,被我注释掉了,如果你的服务器环境是linux系统,可以使用那种格式。如果你的服务器环境是windows系统。可以使用代码里面的格式即可。
自己提前在本地硬盘里创建好对应的文件夹即可。
下一步就是到tomcat的server.xml配置文件里,增加几行代码。
- <Context docBase="D:\videosource" path="/videosrcurl" />
- <Context docBase="D:\xiaoquvideosource" path="/xiaoquvideosrcurl" />
- <Context docBase="D:\newspeituimage" path="/newspeitu" />
- <Context docBase="D:\videopeituimage" path="/videopeitu" />
- <Context docBase="D:\workspeituimage" path="/workspeitu" />
- <Context docBase="D:\chengjipeituimage" path="/chengjipeitu" />
- <Context docBase="D:\teapeituimage" path="/teapeitu" />
- <Context docBase="D:\xiaoqupeituimage" path="/xiaoqupeitu" />
- <Context docBase="D:\proimage" path="/image" />
- <Context docBase="D:\sppeituimage" path="/sppeitu" />
- <Context docBase="zz106ms" path="/zz106ms" reloadable="true"
- source="org.eclipse.jst.jee.server:zz106ms" />
如图所示,这是我在自己d盘里创建好的文件夹,上传的图片视频会存在对应的文件夹里面。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。