SpringBoot + vue的news例子

具体的代码托管在github上:https://github.com/201705010201/SpringBoot-Vue-news-

这里就摘出来一个文件上传的controller

package com.xm.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@CrossOrigin
@RestController
public class FileController {

    @PostMapping("/file")
    public String file(@RequestParam("myfile") MultipartFile myfile,HttpServletRequest request) throws IOException{
         //上传文件是否为空?
        if (!myfile.isEmpty()){
            //获取存放文件的路径
            String path = request.getServletContext().getRealPath("upload");
            //得到上传文件的文件名
            String filename = myfile.getOriginalFilename();
            //取文件扩展名
            String suffix = filename.substring(filename.lastIndexOf("."),filename.length());
            //随机的生存一个32的字符串,作为文件名
            filename = UUID.randomUUID()+suffix;
            
            //创建File对象
            File newFile = new File(path,filename);
            
            //判断文件父目录是否存在
            if(!newFile.getParentFile().exists()){ 
                newFile.getParentFile().mkdir();
            }
 
            //通过CommonsMultipartFile的方法直接写文件
            myfile.transferTo(newFile);
            return "upload/"+filename;
        }else{
            return "";
        }
    }
}

效果图:

扫描二维码关注公众号,回复: 10270663 查看本文章
发布了272 篇原创文章 · 获赞 19 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/hello_cmy/article/details/104874179
今日推荐