springboot 打成jar包 上传图片并保存

springboot打成jar包后获取项目路径为

file:/D:/Document/Document.jar!/BOOT-INF/classes!/static

所以如果打jar包需要处理路径问题

Springboot用io流读取并保存图片到同级目录

 /**
     * 读取图片
     * @param url  图片路径
     * @return
     * @throws IOException
     */ 
public static String getPath(String url) throws IOException{
        //读取图片
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(url));
        //给图片从新命名
        String path=new Date().getTime()+".jpg";
        //获取项目所在物理绝对路径
        String  outUrl = ResourceUtils.getURL("classpath:static").getPath().replace("%20", " ");
        //输出的结果outUrl为:file:/D:/Document/Document.jar!/BOOT-INF/classes!/static
         System.out.println(outUrl);
       
           if(outUrl.contains("file")){
               //获得Document.jar!/在路径中的位置
               int getFile=outUrl.indexOf("file")+6;
                 //去掉file:/
               outUrl=outUrl.substring(getFile,outUrl.length());
           }
           //获得Document.jar!/在路径中的位置
           int result1=outUrl.lastIndexOf("Document.jar!/");
         //去掉Document.jar!/及后面的路径
        outUrl = outUrl.substring(0,result1)+"upload/";
        
        File file=new File(outUrl);
        //判断路径是否存在(file.exists() 如果存在返回true不存在返回false)
        if(!file.exists()){
            //不存在创建文件夹
            file.mkdirs();
        }
        outUrl=outUrl+path;
        //输出结果为D:/Document/upload/1564643609350.jpg
        System.out.println(outUrl);
        //输出到指定目录
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outUrl));

        int i;
        while((i=in.read())!=-1){
            out.write(i);
        }
        out.flush();

        out.close();
        in.close();
        return path;
    }

设置虚拟路径

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * @ProjectName: Document
 * @Package: com.XXXX.util
 * @Author: huat
 * @Date: 2019/7/26 10:18
 * @Version: 1.0
 */


@Configuration
public class WebConfigurer implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;

    // 这个方法是用来配置静态资源的,比如html,js,css,等等
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/**");
       
        String  outUrl =null;
         try{
               //获得项目路径:file:/D:/Document/Document.jar!/BOOT-INF/classes!/static
               outUrl = ResourceUtils.getURL("classpath:static").getPath().replace("%20", " ");

         }catch (Exception e){

         }
        //拿到Document.jar!/在路径中的位置
        int result1=outUrl.lastIndexOf("Document.jar!/");
        //截取掉后面的
        outUrl = outUrl.substring(0,result1)+"upload/";
        //输出结果为file:/D:/Document/upload/
        System.out.println(outUrl);

        //将file:/D:/Document/upload/设置为/upload/**
        //注意虚拟路径设置时不能将file:/去掉
        registry.addResourceHandler("/upload/**").addResourceLocations(outUrl);

       
    }

    // 这个方法用来注册拦截器,我们自己写好的拦截器需要通过这里添加注册才能生效
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns("/**") 表示拦截所有的请求,
        //excludePathPatterns("/login", "/register"); 表示除了登陆与注册之外,因为登陆注册不需要登陆也可以访问
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns("/loginInto", "/login","/login/**","/css/**","/font/**","/img/**","/images/**","/js/**","/lay/**");
        //super.addInterceptors(registry);    //较新Spring Boot的版本中这里可以直接去掉,否则会报错

    }

 

转载于:https://my.oschina.net/u/3535099/blog/3082189

你可能感兴趣的:(java)