码迷,mamicode.com
首页 > 其他好文 > 详细

访问需要指定磁盘路径

时间:2018-09-22 14:40:39      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:用户   random   fail   web   class   ace   rand   成功   stack   

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

 

访问需要指定磁盘路径

标签:用户   random   fail   web   class   ace   rand   成功   stack   

原文地址:https://www.cnblogs.com/Mblood/p/9689887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!