java 图片上传路径问题

package com.zeiot;

import com.zeiot.Core.StaticData;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@MapperScan("com.zeiot.mapper")
@SpringBootApplication
public class XXXXX implements WebMvcConfigurer {

    @Value("${images.win}")
    String winfile;
    @Value("${images.liunx}")
    String liunxfile;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        String os=System.getProperty("os.name");

        //如果是Windows系统
        if (os.toLowerCase().startsWith("win")) {
            StaticData.ImagesPath=winfile;
            registry.addResourceHandler("/images/**")
                    // /app_file/**表示在磁盘filePathWindow目录下的所有资源会被解析为以下的路径
                    .addResourceLocations("file:" + winfile);
        } else {  //linux 和mac
            StaticData.ImagesPath=liunxfile;
            registry.addResourceHandler("/images/**")
                    .addResourceLocations("file:"+liunxfile ) ;
        }

        // 添加静态资源路径,然后跟项目的路径进行映射
//        registry.addResourceHandler("/images/**").addResourceLocations("classpath:/static/");
    }
    public static void main(String[] args) {
        SpringApplication.run(ZeiotRosApiApplication.class, args);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,进行图片时,需要指定服务器上的保存路径来存储上图片。一种常见的设置保存路径的方法是通过配置文件来进行配置。具体步骤如下所示: 首先,在配置文件中设置保存路径的属性,可以命名为"upload.path",该属性的值就是保存图片路径。例如,可以设置为"upload.path=D:/uploads/"。 然后,在Java代码中读取配置文件中的属性值,并使用该值作为保存路径。可以使用Properties类来读取配置文件的内容。例如,可以使用以下代码来读取保存路径: ```java Properties properties = new Properties(); try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("config.properties")) { properties.load(inputStream); } catch (IOException e) { e.printStackTrace(); } String savePath = properties.getProperty("upload.path"); ``` 最后,可以通过以下代码来保存上图片到指定的路径: ```java Part filePart = request.getPart("file"); // 获取上的文件 String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // 获取文件名 String filePath = savePath + fileName; // 拼接保存路径和文件名 try (InputStream inputStream = filePart.getInputStream(); FileOutputStream outputStream = new FileOutputStream(filePath)) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } } ``` 上述代码中,通过request.getPart("file")获取上的文件,并通过getSubmittedFileName()方法获取文件名。然后,通过FileOutputStream将文件保存到指定的路径。 以上就是使用Java进行图片时,设置服务器保存路径的方法。根据实际需求,可以灵活地配置保存路径,并通过代码将上图片保存到指定的路径

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值