springboot上传文件与回显

硅谷探秘者 2027 0 0

springboot上传文件与回显


资源映射路径配置:

package com.dzqc.yx.controller;
 
import org.springframework.beans.factory.annotation.Value;
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 MyWebAppPhotoConfigurer implements WebMvcConfigurer {
 
    @Value("${message.photo_dir}")
    private String photo_dir;
 
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        System.err.println(photo_dir);
        registry.addResourceHandler("/photo/**").addResourceLocations("file:"+photo_dir);
    }
}

用户上传的图片可以在/photo/**路径下访问


磁盘路径地址在.yml配置文件中设置:

message:
  md5-key: yx
  photo_dir: D:/photo/


上传文件的控制器

@RequestMapping("/binding3")
    @ResponseBody
    public AjaxResult binding3(HttpSession session,
            @RequestParam(value="file",required = true) MultipartFile file){
        if(file.isEmpty()){
            return AjaxResult.fail("请选择文件");
        }
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(file.getInputStream());
            if(bi==null){
                return AjaxResult.fail("您上传的不是图片格式");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.fail("您上传的不是图片格式");
        }
        File path = new File(photo_dir);
        if(!path.exists()) {
            path = new File("");
        }
        String fileName=file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        File f=new File(path.getAbsolutePath(), UUID.randomUUID().toString()+suffix);
        try {
            file.transferTo(f);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("文件保存异常");
        }
        return AjaxResult.success("上传成功:"+f.getName());
}

文件存放在D:/photo/目录下


menu.saveimg.savepath20190419161241.jpg


menu.saveimg.savepath20190419161329.jpg




评论区
请写下您的评论...
暂无评论...
猜你喜欢
框架 2761 $FileSizeLimitExceededException:Thefieldfileexceedsitsmaximumpermittedsizeof1048576bytes.yml加配置springboot2.0配置spring:servlet:multipart:max-file-s
工具 1922 服务端(接受) /** *接口 *@paramtype *@return */ @RequestMapping(value="/upFile",method
minio 2275 创建buckets 创建serviceaccounts java maven依赖: dependency groupIdio.minio/groupId
工具 2447 pom依赖dependencygroupIdcommons-net/groupIdartifactIdcommons-net/artifactIdversion3.6/version/dependencyFTP服务类packageclub.jiajiajia.bulider.service;importjava.io.IOException;importjava.io.InputStream;im
前端(h5) 2751 后端这么写packagecn.com.dzqc.controller;importjavax.servlet.http.HttpServletRequest;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.sp
框架 7729 一次实现的进度条原理:在之前,请求以下服务器,获取一个socketId(作用:用来标识本次连接,服务器端也是通过这个id找到对应的连接,然后给这个连接发送消息(进度)),此时socket
linux系统 1564 nginx代理报413(RequestEntityTooLarge)错误的解决方案RequestEntityTooLarge:请求的实体太大,nginx代理时默认的大小是1M解决方案:设置
框架 4986 1.修改ueditor.config.js配置修改如下配置:serverUrl:"/exam/ueditor/initController"2.创建配置接口
归档
2018-11  12 2018-12  33 2019-01  28 2019-02  28 2019-03  32 2019-04  27 2019-05  33 2019-06  6 2019-07  12 2019-08  12 2019-09  21 2019-10  8 2019-11  15 2019-12  25 2020-01  9 2020-02  5 2020-03  16 2020-04  4 2020-06  1 2020-07  7 2020-08  13 2020-09  9 2020-10  5 2020-12  3 2021-01  1 2021-02  5 2021-03  7 2021-04  4 2021-05  4 2021-06  1 2021-07  7 2021-08  2 2021-09  8 2021-10  9 2021-11  16 2021-12  14 2022-01  7 2022-05  1 2022-08  3 2022-09  2 2022-10  2 2022-12  5 2023-01  3 2023-02  1 2023-03  4 2023-04  2 2023-06  3 2023-07  4 2023-08  1 2023-10  1 2024-02  1 2024-03  1 2024-04  1
标签
算法基础 linux 前端 c++ 数据结构 框架 数据库 计算机基础 储备知识 java基础 ASM 其他 深入理解java虚拟机 nginx git 消息中间件 搜索 maven redis docker dubbo vue 导入导出 软件使用 idea插件 协议 无聊的知识 jenkins springboot mqtt协议 keepalived minio mysql ensp 网络基础 xxl-job rabbitmq haproxy srs 音视频 webrtc javascript
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。