第一步:获取本地文件夹中的所有图片路径
springboot默认可以访问resources下的static文件夹下的静态资源,我们一般将图片指定上传到static下的某个文件夹,例如images,开发阶段可以使用,但是当项目打成jar包就无法使用,运行会报出无法找到文件路径。这时候就需要配置虚拟路径,用来指定到硬盘下的固定地址。
1 SpringBoot配置虚拟化路径用于图片的展示
package baobaobaobao
.config
;
import org
.springframework
.context
.annotation
.Configuration
;
import org
.springframework
.web
.servlet
.config
.annotation
.ResourceHandlerRegistry
;
import org
.springframework
.web
.servlet
.config
.annotation
.WebMvcConfigurer
;
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry
) {
System
.out
.println("静态请求通过虚拟路径到外部资源映射");
registry
.addResourceHandler( "/view/**").addResourceLocations("file://");
}
}
2dao层
package baobaobaobao
.dao
;
import baobaobaobao
.entity
.FilePath
;
import java
.util
.List
;
public interface FilePathMapper {
int picture(FilePath filePath
);
List
<String
> path();
}
3service层
package baobaobaobao
.service
;
import baobaobaobao
.entity
.FilePath
;
import java
.util
.List
;
public interface FilePathServiceFace {
int picture(FilePath filePath
);
List
<String> path();
}
package baobaobaobao
.service
;
import baobaobaobao
.dao
.FilePathMapper
;
import baobaobaobao
.entity
.FilePath
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Service
;
import java
.util
.List
;
@Service
public class FilePathService implements FilePathServiceFace {
@Autowired
public FilePathMapper filePathMapper
;
public FilePathMapper
getFilePathMapper() {
return filePathMapper
;
}
public void setFilePathMapper(FilePathMapper filePathMapper
) {
this.filePathMapper
= filePathMapper
;
}
@Override
public int picture(FilePath filePath
) {
int picture
= filePathMapper
.picture(filePath
);
return picture
;
}
@Override
public List
<String> path() {
List
<String> path
= filePathMapper
.path();
return path
;
}
}
4 controller层
package baobaobaobao
.controller
;
import baobaobaobao
.service
.FilePathServiceFace
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Controller
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
import org
.springframework
.web
.bind
.annotation
.RequestMethod
;
import org
.springframework
.web
.bind
.annotation
.ResponseBody
;
import org
.springframework
.web
.servlet
.ModelAndView
;
import java
.util
.List
;
@Controller
public class ManyFileloadController {
@Autowired
public FilePathServiceFace filePathService
;
public FilePathServiceFace
getFilePathService() {
return filePathService
;
}
public void setFilePathService(FilePathServiceFace filePathService
) {
this.filePathService
= filePathService
;
}
@ResponseBody
@RequestMapping(value
= "path",method
= {RequestMethod
.GET
,RequestMethod
.POST
})
public List
<String> view(){
ModelAndView modelAndView
= new ModelAndView();
modelAndView
.setViewName("WEB-INF/view");
List
<String> path
= filePathService
.path();
return path
;
}
}
第二步 前端ajax调用请求获取图片路径数组
<%--
Created by IntelliJ IDEA
.
User
: Administrator
Date
: 2020/6/22
Time
: 11:51
To change
this template use File
| Settings
| File Templates
.
--%>
<%@ page contentType
="text/html;charset=UTF-8" language
="java" %>
<html>
<head>
<title>Title
</title
>
<script type
="text/javascript" src
="js/jquery-2.1.1.min.js"></script
>
<script type
="text/javascript">
if (top
.location
!== self
.location
) {
top
.location
=self
.location
;
}
</script
>
</head
>
<body>
<div id
="shuang">
<button onclick
="common_getPicFileList()" style
="width:1666px; height:666px; background-color: #e4b9b9;text-align: center ">查看历史记录
</button
>
</div
>
<script type
="text/javascript">
function
common_getPicFileList() {
$
.ajax({
url
: "path",
type
: "POST",
success
: function
(data
) {
if (!data
.length
) {
alert("data出错");
return;
} else {
loadPicFormDB(data
);
}
},
error
: function
(e
) {
console
.log(e
);
console
.log("获取文件list数组失败,请检查接口服务");
}
});
}
function
loadPicFormDB(data
) {
var pichtml
= "";
for (var i
= 0; i
< data
.length
; i
++) {
var src
=data
[i
];
var html1
= '<li><a href="'+ src
+ '" rel="lightbox" title="' + data
[i
] + '" target="_blank">'
+ '<img οnlοad="AutoResizeImage(800,450,this)" src="view/' + src
+ '"></a><span>' + data
[i
] + '</span></li>';
pichtml
+= html1
;
}
;
showPic(pichtml
);
}
function
AutoResizeImage(maxWidth
, maxHeight
, objImg
) {
var img
= new Image();
img
.src
= objImg
.src
;
var hRatio
;
var wRatio
;
var Ratio
= 1;
var w
= img
.width
;
var h
= img
.height
;
wRatio
= maxWidth
/ w
;
hRatio
= maxHeight
/ h
;
if (maxWidth
== 0 && maxHeight
== 0) {
Ratio
= 1;
} else if (maxWidth
== 0) {
if (hRatio
< 1)
Ratio
= hRatio
;
} else if (maxHeight
== 0) {
if (wRatio
< 1)
Ratio
= wRatio
;
} else if (wRatio
< 1 || hRatio
< 1) {
Ratio
= (wRatio
<= hRatio
? wRatio
: hRatio
);
}
if (Ratio
< 1) {
w
= w
* Ratio
;
h
= h
* Ratio
;
}
objImg
.height
= h
;
objImg
.width
= w
;
}
var baseText
= null
;
function
showPic(pichtml
){
var popUp
= document
.getElementById("shuang");
popUp
.innerHTML
= pichtml
}
</script
>
</body
>
</html
>