Srpin Boot实现文件上传

2020-02-07T02:02:00
@Controller
public class UploadController {

    @RequestMapping("upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException {
        String exName= file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String fileName = UUID.randomUUID().toString()+exName;

        
        FileCopyUtils.copy(file.getInputStream(),new FileOutputStream(new File("D:\\data\\"+fileName)));
        
        System.out.println(file);
        return fileName;
    }

}

前端代码

            <form action="/jobadmin/jobregister/upload" method="post" enctype="multipart/form-data">
                <div  class="col-xs-2">
                    <input  type="file" name="filePath">
                </div>
                <div class="col-xs-2">
                    <input class="btn btn-block btn-success" type="submit" value="文件上传">
                </div>
            </form>
当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »