SSM学习记录8:通过axios上传文件并在网页得到服务器中的图片(注解方式)

之前所做的注解配置不动,只需在ServletConfig中注册MultipartConfig便可

import jakarta.servlet.MultipartConfigElement;
import jakarta.servlet.ServletRegistration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    //在此进行注册MultipartConfig↓
    //MultipartConfigElement(String location, long maxFileSize, long maxRequestSize, int fileSizeThreshold)
    //(存储位置,最大单个文件大小,最大请求大小(在单个请求中有多个文件的情况下),将文件上传进度刷新到存储位置的大小)
    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        registration.setMultipartConfig(
                new MultipartConfigElement("",1024*1024*500,1024*1024*500,0)
        );
    }
}

在js中发送axios请求↓

let formData = new FormData();//需要将文件放入表单数据类中
//使用键值对的方式将数据加入表单数据,取值时通过键名file取出文件
//fileData是通过文件选择器或其他方式得到的文件
//         "/files"是controller拦截的路径
formData.append("file", fileData, "上传文件");
axios.post("/files", formData).then(res=>{
console.log(res)//成功后返回的信息
          })

通过controller接收上传的文件↓

import domain.Code;
import domain.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;


@RestController
@RequestMapping("/files")
public class FileLoadController {

    private String prefix = "C:/Users/17512/Desktop/毕设/";
    private String infix = null;
    private String suffix = null;
    private String UPLOAD_COVER_DIRECTORY = "spCoverImage";
    private String filePath = null;
    private byte[] bytes = null;
    private int len = 0;

    @PostMapping
    public Result saveUploadFile(@RequestParam("file") MultipartFile multipartFile) throws IOException {

        //System.out.println(multipartFile.getSize());
        FileOutputStream fileOutputStream = null;
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        
        // 构造绝对路径来存储上传的文件
        infix = UPLOAD_COVER_DIRECTORY;//中缀,要保存文件的文件夹名,这是我项目中需要的
        suffix = multipartFile.getOriginalFilename();//后缀,得到formData里的第三个参数即文件名

        // 如果目录不存在则创建
        File uploadDir = new File(prefix + infix);//前缀+中缀创建保存文件的文件夹
        if (!uploadDir.exists()) uploadDir.mkdir();
        
        filePath = uploadDir.getAbsolutePath() + File.separator + suffix;//最终文件名
        
        //System.out.println(filePath);
        bufferedInputStream = new BufferedInputStream(multipartFile.getInputStream());
        fileOutputStream = new FileOutputStream(filePath);
        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        try {
            //System.out.println("开始转存");
            bytes = new byte[1024];
            len = bufferedInputStream.read(bytes, 0, bytes.length);
            while (len != -1) {
                bufferedOutputStream.write(bytes, 0, len);
                len = bufferedInputStream.read(bytes, 0, bytes.length);
            }

        } finally {
            bufferedOutputStream.close();
            fileOutputStream.close();
            bufferedInputStream.close();
        }
        //System.out.println("转存完成");
        return new Result(Code.UPLOAD_OK, "上传成功!", filePath);
    }
}

用户端显示服务器图片↓
在SpringMvcSupport中注册资源处理器,跟之前学习的给资源放行同理

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class SpringMvcSupprot extends WebMvcConfigurationSupport {
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**").addResourceLocations("/img/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        //在此注册用户端访问图片路径↓  注:file:不可少,表示绝对路径
        registry.addResourceHandler("/spCoverImage/**").addResourceLocations("file:C:/Users/17512/Desktop/毕设/spCoverImage/");

    }
}

html中代码如下↓这里我导入了VUE,不使用vue的项目将src前面的:去掉

<img id="取个名" width="100%" :src="dialogImageUrl" alt="">

js中代码↓

//window.location.href将得到当前http地址,例 http://localhost:8080/
//window.location.href可以不用,img中的src请求路径为spCoverImage/景点测试_12.jpg也会被controller直接拦截后在资源路径中寻找名为景点测试_12.jpg的文件
this.dialogImageUrl = window.location.href + 'spCoverImage/景点测试_12.jpg';

当同一个img,其src不改变,但是文件内容却改变了,想要将img刷新显示,我们可以在给src的路径后加上一个无意义的参数,例↓,这样就能在src不变的情况下,刷新img的显示

document.getElementById("取个名").src = spCoverImage/景点测试_12.jpg+ "?i=" + Math.random();
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

倚肆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值