图片上传jar包同级目录下创建upload文件

WebMvcConfigurer配置

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
    @SneakyThrows
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //和页面有关的静态目录都放在项目的static目录下
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/Image/**").addResourceLocations("file:Image/");
       registry.addResourceHandler("/upload/**").addResourceLocations("file:upload/");
    }
}
/**
* 临时上传存储空间
* @author zhangshichuan
* @date 2021/2/26 11:05
*/
@Configuration
public class UploadConfig {

    @Bean
    public MultipartConfigElement multipartConfigElement(){
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setLocation(System.getProperty("user.dir")+"/uploadimg/");
        return factory.createMultipartConfig();
    }
}
	@RequestMapping("uploadimg")
    @ResponseBody
    public Map<String, Object> upload(MultipartFile file, HttpServletRequest request) {
        Map<String, Object> map = new HashMap();
        Map<String,Object> map2 = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        Date date =calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String stringDate = sdf.format(date);

        String newName = UUID.randomUUID().toString().replace("-","");
        System.out.println("newName:"+newName);
        try {
            //在jar包的同级目录下创建image文件夹
            String name = file.getOriginalFilename();
            String fileName = newName +name.substring(name.lastIndexOf("."), name.length());
            String path = System.getProperty("user.dir")+"/upload/"+stringDate+"/";
            System.out.println("path:"+path);
            File filePath = new File(path+fileName);
            System.out.println("filePath:"+filePath);
            if (!filePath.getParentFile().exists()){
                filePath.getParentFile().mkdirs();
            }
            FileCopyUtils.copy(file.getInputStream(),new FileOutputStream(filePath));
            String imgName = "/upload/"+stringDate+"/"+fileName;
            /*//得到协议名 例如:https://localhost:8090/
            String PP =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/static/upload/";*/
            map.put("code", 0);
            map.put("msg", "上传成功");
            /*map2.put("src",PP+name);*/
            map2.put("src",imgName);
            map2.put("title",file.getOriginalFilename());
            map.put("data",map2);
        }catch (Exception e){
            System.out.println(e);
            map.put("code", 1);
            map.put("msg", e);
            map2.put("src", "");
            map2.put("title","");
            map.put("data", "");
        }
        return map;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot项目中实现负载均衡,并将上传的文件保存在服务器目录中,需要遵循以下几个步骤: 1.创建Spring Boot项目:首先,需要创建一个Spring Boot项目。可以选择使用IDE工具创建一个基本模板,并在pom.xml文件中添加必要的依赖库(例如Spring Web和Spring Boot DevTools)。 2.实现文件上传功能:在Spring Boot项目中实现文件上传功能,可以使用Spring的MultipartFile类,它使得上传文件变得简单。要将上传的文件保存在服务器目录中,需要一些常规的Java文件IO操作。在控制器中,可以通过使用@requestMapping注释处理文件上传请求,并用@Value注释注入目标目录。 3.设置负载均衡:使用Spring Cloud集成负载均衡器,可以轻松地将请求转发给多个服务,从而可提高系统的可用性。使用Spring Cloud负载均衡器,需要从pom.xml文件中添加相应的依赖。在Spring Boot项目中使用负载均衡器时,需要使用Spring Cloud的@LoadBalanced注释实现“客户端负载均衡”。 4.保存文件目录:将上传的文件保存到目录jar包同级,可以使用Java中的路径工具类,例如Path和Files类实现。也可以使用Spring Boot提供的特殊类PathUtils,将文件路径设置为“classpath:/static”即可。 综上,可以采取以上步骤,并使用Spring Cloud集成负载均衡器应对大量请求,实现将上传的文件保存在服务器目录jar包同级

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值