访问需要指定磁盘路径

Posted mblood

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了访问需要指定磁盘路径相关的知识,希望对你有一定的参考价值。

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","上传失败");
    }
}

 



以上是关于访问需要指定磁盘路径的主要内容,如果未能解决你的问题,请参考以下文章