访问需要指定磁盘路径

application.properties中增加下面配置
1) web.images-path=/Users/jack/Desktop
2) spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.images-path}

代码调用:

@RestController
@PropertySource({"classpath:application.properties"})
public class FileController {


    @Value("${web.images-path}")
    private String filePath;

//    @Autowired
//    private ServiceSetting serviceSetting;

    //private String path="C:\\Users\\54701\\Desktop\\images";

    @RequestMapping(value = "uploadfile")
    public JsonData upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest request){

        String name=request.getParameter("name");
        System.out.println("用户名:"+name);

        //获取文件名
        String fileName=file.getOriginalFilename();
        System.out.println("上传的文件名为:"+fileName);

        //获取文件的后缀名
        String suffixName=fileName.substring(fileName.lastIndexOf("."));
        System.out.println("上传的后缀名为:"+suffixName);

        //文件上传后的路径
        fileName= UUID.randomUUID()+suffixName;
        //File dest=new File(serviceSetting.getName()+fileName);
        File dest=new File(filePath+fileName);

        try {
            file.transferTo(dest);
            return new JsonData(0,fileName,"上传成功");
        }catch (IllegalStateException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return new JsonData(-1,"fail","上传失败");
    }
}

猜你喜欢

转载自www.cnblogs.com/Mblood/p/9689887.html