南栒 2023-05-15 22:41 采纳率: 0%
浏览 34

springboot上传到服务器的图片不显示

springboot中传到服务器的图片无法显示

img


```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 &nbsp;&nbsp;&nbsp;</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> &nbsp;&nbsp;
                 <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">
         上传文件:&nbsp;&nbsp;<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>

  • 写回答

1条回答 默认 最新

  • 瞬间的未来式 2023-05-15 22:58
    关注

    1.到目录下看图片是否上传成功
    2.看springboot配置的访问目录是否正确

    评论

报告相同问题?

问题事件

  • 创建了问题 5月15日

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里