SpringMVC上传图片与访问
作者:201200834201 时间:2023-12-06 17:54:31
关于springmvc上传图片的方法小编给大家整理了两种方法,具体内容如下所示:
第一种:(放在该项目下的物理地址对应的位置)
a. 路径写法:
String basePath="/WEB-INF/resources/upload";
String filePathName= request.getSession().getServletContext().getRealPath(basePath);存放路径
b. 实际路径:
D:\WorkSpace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\XYT\WEB-INF\resources\upload\图片名称
c. 访问路径: http://localhost:8080/XYT/resources/upload/图片名称
d. 前提:只要这个项目能运行就行。
第二种:(创建虚拟路径,配置Tomcat下server.xml,创建存储路径和访问路径)
1.路径写法:
String filePathName=Constant.IMG_PATH+File.separator+"upload";
其中:public static final String IMG_PATH = "E:\\Java\\img";
2.路径配置:
Server.xml配置
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<!-- add(save pictures) -->
<Context path="/upload" docBase="E:\Java\img\upload"></Context>
</Host>
3.实际路径:E:\Java\img\upload
4.访问路径:http://localhost:8080/upload/图片名称
5.参考:http://my.oschina.net/pingdy/blog/381001
6.前提:必须打开Tomcat服务器
举例:上传图片的实例:(可以上传多张图片)
JSONObject rs=new JSONObject();
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
String url="";
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext()) {
MultipartFile file = multiRequest.getFile((String) iter.next());
if (file != null) {
String originalFileName = file.getOriginalFilename();
String[] f = originalFileName.split("\\.");
String ext = "";
if(f!=null && f.length>1){
ext = f[f.length-1];
System.out.println(ext);
}
System.out.println(allowImgType==null);
if(!allowImgType.contains(ext.toUpperCase())){
rs.put("code", "ERR_UPLOAD_0003");
rs.put("msg", "类型错误");
return rs;
}
//String basePath="/WEB-INF/resources/upload";//String filePathName = request.getSession().getServletContext().getRealPath(basePath);
String filePathName=Constant.IMG_PATH+File.separator+"upload";
url = filePathName;
System.out.println(url);
//上传后记录在path这个路径下。
File localFile = new File(filePathName);
if(!localFile.exists()){
localFile.mkdir();
}
//compress
String fname =new Date().getTime()+"."+ext;
String originalFname = fname.substring(0,fname.indexOf("."))+"_original."+ext;
String fileName = filePathName + File.separator + fname;
String oFileName = filePathName + File.separator + originalFname;
File infile = new File(fileName);
File oFile = new File(oFileName);
try{
ImageHelper.compress(file.getInputStream(), 600, infile);
file.transferTo(oFile);//original 上传原图
JSONObject obj = new JSONObject();
rs.put("code", Constant.CODE_SUCCESS);
rs.put("data", obj.toString());
}catch(Exception e){
rs.put("code", "ERR_UPLOAD_0001");
rs.put("msg", "ERR_UPLOAD_0001");
e.printStackTrace();
return rs;
}
}
以上所述是针对SpringMVC上传图片与访问的相关内容,希望对大家有所帮助。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java面试题冲刺第二十九天--JVM3
![](https://img.aspxhome.com/file/2023/8/82308_0s.jpg)
SpringBoot如何注册Servlet、Filter、Listener的几种方式
mybatis的大于小于号转义符号一览
![](https://img.aspxhome.com/file/2023/7/60667_0s.png)
Android ListView组件详解及示例代码
![](https://img.aspxhome.com/file/2023/8/90118_0s.png)
详解获取Spring MVC中所有RequestMapping以及对应方法和参数
java实现快速排序图文详解
![](https://img.aspxhome.com/file/2023/3/65363_0s.jpg)
C#事件标准命名规则及说明(包括用作事件类型的委托命名)
Android三方依赖冲突Gradle中exclude的使用
![](https://img.aspxhome.com/file/2023/5/87335_0s.png)
Java实现经典游戏Flappy Bird的示例代码
![](https://img.aspxhome.com/file/2023/5/88155_0s.png)
springboot 整合 sa-token简介及入门教程
![](https://img.aspxhome.com/file/2023/7/83457_0s.png)
C#开发Windows UWP系列之布局面板RelativePanel
![](https://img.aspxhome.com/file/2023/4/81134_0s.png)
java参数传递之值传递和引用传递
java音乐播放器实现代码
![](https://img.aspxhome.com/file/2023/7/91667_0s.jpg)
浅谈图片上传利用request.getInputStream()获取文件流时遇到的问题
![](https://img.aspxhome.com/file/2023/9/64029_0s.jpg)
详解Java编程中JavaMail API的使用
基于spring cloud多个消费端重复定义feign client的问题
C#中面向对象编程机制之继承学习笔记
springboot中自定义异常以及定制异常界面实现过程解析
![](https://img.aspxhome.com/file/2023/3/105913_0s.png)