3.5SpringBoot之文件上传

3.5SpringBoot之文件上传,第1张

1.单文件上传到服务器

index.html




    
    Title


fileController

@RestController
public class fileuploadController {
    /**
     * 按日期分类
     */
    SimpleDateFormat sdf=new SimpleDateFormat("/yyyy/MM/dd/");
    @RequestMapping("/upload")
    public String upliad(MultipartFile file, HttpServletRequest req){
        //获取临时目录
        String realpath=req.getServletContext().getRealPath("/");
        //最终路径
        String format = sdf.format(new Date());
        String path=realpath+ format;
        File folder=new File(path);
        if (!folder.exists()){
            folder.mkdirs();
        }
        String oldName = file.getOriginalFilename();
        String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
        try {
            file.transferTo(new File(path+newName));
            String newName1 =req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+format+ newName;
            return newName1;
//            return path;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}

返回结果

打开链接就可以看见上传的文件

2.单文件上传到本地

    @RequestMapping("/upload")
    public String upload2(@RequestBody MultipartFile file, HttpServletRequest req) throws IOException {//MultipartFile 接收前端传过来的文件
        String format = sdf.format(new Date());
        String path="D:/img"+format;
        File folder=new File(path);
        if (!folder.exists()){
            folder.mkdirs();
        }
        String oldName = file.getOriginalFilename();
        String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
        file.transferTo(new File(path+newName));
        return path+ newName;
    }

多文件上传

1.一次性选择多个文件

index.html




    
    Title


 updaloadController

    SimpleDateFormat sdf=new SimpleDateFormat("/yyyy/MM/dd/");
    @RequestMapping("/uploads2")
    public String upliad(MultipartFile[] files, HttpServletRequest req){
        //获取临时目录
        String realpath=req.getServletContext().getRealPath("/");
        //最终路径
        String format = sdf.format(new Date());
        String path=realpath+ format;
        File folder=new File(path);
        if (!folder.exists()){
            folder.mkdirs();
        }
        try {
        for (MultipartFile file:files){
            String oldName = file.getOriginalFilename();
            String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
            file.transferTo(new File(path+newName));
            String newName1 =req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+format+ newName;
            System.out.println(newName1);
        }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

 2.选择多次

index.html

 SimpleDateFormat sdf=new SimpleDateFormat("/yyyy/MM/dd/");
    @RequestMapping("/uploads3")
    public String upliad(MultipartFile files1,MultipartFile files2, HttpServletRequest req){
        //获取临时目录
        String realpath=req.getServletContext().getRealPath("/");
        //最终路径
        String format = sdf.format(new Date());
        String path=realpath+ format;
        File folder=new File(path);
        if (!folder.exists()){
            folder.mkdirs();
        }
        try {

            String oldName = files1.getOriginalFilename();
            String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
            files1.transferTo(new File(path+newName));
            String s1 =req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+format+ newName;
            System.out.println(s1);

            String oldName2 = files2.getOriginalFilename();
            String newName2= UUID.randomUUID().toString()+oldName2.substring(oldName2.lastIndexOf("."));
            files2.transferTo(new File(path+newName2));
            String s2 =req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+format+ newName2;
            System.out.println(s2);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

 updaloadController




    
    Title


ajax上传

ajax.html




    
    
    Title



    
    



uploadController

 SimpleDateFormat sdf=new SimpleDateFormat("/yyyy/MM/dd/");
    @RequestMapping("/upload")
    public String upliad(MultipartFile file, HttpServletRequest req){
        //获取临时目录
        String realpath=req.getServletContext().getRealPath("/");
        //最终路径
        String format = sdf.format(new Date());
        String path=realpath+ format;
        File folder=new File(path);
        if (!folder.exists()){
            folder.mkdirs();
        }
        String oldName = file.getOriginalFilename();
        String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
        try {
            file.transferTo(new File(path+newName));
            String newName1 =req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+format+ newName;
            return newName1;
//            return path;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/796027.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存