springboot中传到服务器的图片无法显示
```javascript
@Controller
public class FileController {
@GetMapping("/toUpload")
public String toUpload(){
return "upload";
}
@PostMapping("uploadFile")
public String uploadFile(MultipartFile[] fileUpload, Model model){
model.addAttribute("uploadStatus","上传成功");
for (MultipartFile file :fileUpload){
String fileName =file.getOriginalFilename();
fileName = UUID.randomUUID()+"_"+fileName;
String dirPath="D:/file/";
File filePath = new File(dirPath);
if(!filePath.exists()){
filePath.mkdirs();
}
try {
file.transferTo(new File(dirPath+fileName));
}catch ( Exception e){
e.printStackTrace();
model.addAttribute("uploadStatus","上传失败"+e.getMessage());
}
}
return "upload";
}
@Autowired(required = false)
private CommentMapper commentMapper;
@PostMapping("/uploadCommentFigure")
public String uploadFile(HttpServletRequest request, Model model){
model.addAttribute("uploadStatus", "上传成功!");
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartHttpServletRequest.getFile("fileUpload");
String filename = file.getOriginalFilename();
filename = UUID.randomUUID() + "_" + filename;
Resource resource = new ClassPathResource("static\\");
try{
String uploadFilePath = resource.getFile().getPath() + "\\login\\img\\";
file.transferTo(new File(uploadFilePath+filename));
String recordId = request.getParameter("uploadFigureRowId");
Comment comment = commentMapper.findById(Integer.parseInt(recordId));
comment.setFigure("\\login\\img\\" + filename);
System.out.println(comment);
commentMapper.updateCommentFigure(comment);
}catch (Exception e){
e.printStackTrace();
model.addAttribute("uploadStatus","上传失败!" + e.getMessage());
}
model.addAttribute("comments", commentMapper.findAllComments());
return "displayComments";
}
}
```css
<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body {width: 100% }
div {width: 20px; margin: 0 auto;}
h2 { text-align: center}
table {border-collapse: collapse}
table th { border: 1px solid black}
table td{
border: 1px solid black;
text-align: center;
}
</style>
<script th:src="@{/login/js/jquery.min.js}"></script>
<script type="text/javascript">
function showUploadFileDiv(rowId){
$("#uploadFileDiv").show();
$("#uploadFigureRowId").attr("value",rowId);
}
function hideText(rowId){
$("#uploadFileDiv").show();
$("#uploadFigureRowId").attr("value",rowId);
$(mySelector1).hide();
$(mySelector2).show();
}
function changeStyle(rowId){
$("#rowId").attr("text-decoration","underline");
$("#rowId").attr("color","darkblue");
}
function add(){
var innerdiv="<div>";
innerdiv +="<input type='file' name='fileUpload' required='required'>"+
+"<input type='button' value='删除' onclick='remove(this)'>";
innerdiv +="</div>";
$("#file").append(innerdiv);
$("#submit").css("display","block");
}
function remove(obj){
$(obj).parent().remove();
if($("#file div").length==0){
$("#submit").css("display","none");
}
}
</script>
</head>
<body>
<div>
<h2>Comments </h2>
<table>
<tr>
<th>id</th>
<th>content</th>
<th>author</th>
<th>aId</th>
<th style="width:100px;">deal</th>
<th style="width:120px;">figure</th>
</tr>
<tr th:each= "comment:${comments}">
<td th:text="${comment.id}"></td>
<td th:text="${comment.content}"></td>
<td th:text="${comment.author}"></td>
<td th:text="${comment.aId}"></td>
<td>
<a th:href="@{'/deleteCurrentRecord/'+'?currentCommentId=' + ${comment.id}}">
<span id="processDeletion" style="...">执行删除</span>
</a>
<span style="..."th:onclick="preHandleDeletion([${comment.id}])">删除</span>
<a href="#">编辑</a>
</td>
<td>
<span th:id="${comment.id}" th:if="${comment.figure} eq null" style="color:blue;" onmouseenter="changeStyle(this.id)" onclick="showUploadFileDiv(this.id)">上传配图</span>
<span th:if="${comment.figure} ne null"> <imp height=90px style="display:none;" th:name="${comment.id}" th:src="${comment.figure}" onload="hideText(this.name)"/></span>
</td>
</tr>
</table>
</div>
<div id="uploadFileDiv" style="display:none;">
<form th:action="@{/uploadCommentFigure}"method="post" enctype="multipart/form-data">
上传文件: <input type="button" value="添加文件" onclick="add()"/>
<div id="file" style="margin-top:10px;" th:value="文件上传区域"></div>
<input id="uploadFigureRowId" name="uploadFigureRowId" type="hidden"/>
<input id="submit" type="submit" value="上传"
style="display:none;marign-top:10px;"/>
</form>
</div>
</body>
</html>