图片上传linux服务器并获取

@ApiOperation(value = "多图上传")
    @RequestMapping("/uploadImgList")
    @ResponseBody
    public String uploadPictureList(@RequestParam(value="file",required=false)MultipartFile[] file, HttpServletRequest request){
        File targetFile=null;
        String msg="";//返回存储路径
        int code=1;
        List imgList=new ArrayList();

        System.out.println("图片张数---:"+file.length);
        if (file!=null && file.length>0) {
            for (int i = 0; i < file.length; i++) {
                String fileName=file[i].getOriginalFilename();//获取文件名加后缀
                if(fileName!=null&&fileName!=""){
                    String returnUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() +"/upload/imgs/";//存储路径
                    //String path = request.getSession().getServletContext().getRealPath("upload/imgs"); //文件存储位置
//                    String path=System.getProperty("user.dir")+"/upload";   //当前项目服务器根目录路径
                    String path=System.getProperty("user.dir")+"/upload";
                    String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());//文件后缀
                    fileName=new Date().getTime()+"_"+new Random().nextInt(1000)+fileF;//新的文件名

                    //先判断文件是否存在
                    String fileAdd = DateUtil.format(new Date(),"yyyy-MM-dd");
                    File file1 =new File(path+"/"+fileAdd);
                    System.out.println("文件路径---:"+file1);

                    targetFile = new File(file1, fileName);
                    //如果文件夹不存在则创建
                    if (!targetFile.getParentFile().exists()) {
                        targetFile.getParentFile().mkdirs();
                    }
                    try {
                        file[i].transferTo(targetFile);
                        msg="/"+fileName;
                        imgList.add(msg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return JSONUtil.toJsonStr(imgList);
    }

    @ApiOperation("单图上传")
    @RequestMapping("/upload")
    @ResponseBody
    public String handleFileUpload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "文件为空";
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());//文件后缀
        fileName=new Date().getTime()+"_"+new Random().nextInt(1000)+fileF;//新的文件名
//        // 获取文件的后缀名
//        String suffixName = fileName.substring(fileName.lastIndexOf("."));
//        // 文件上传后的路径
//        String str = "/upload/"+DateUtil.format(new Date(),"yyyy-MM-dd")+"/";
        //部署项目当前目录下创建upload文件夹
//        String filePath=System.getProperty("user.dir")+"/upload";
        //docker部署指定挂载目录,不然无法访问
        String filePath = "/usr/local/nginx/upload/";//绝对路径
        String url = filePath + fileName;
        File dest = new File(url);
        System.out.println("文件地址----:"+dest);

        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            return fileName;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }

    /**
     * 读取源文件名,修改为不重复的文件名
     * @param originalFilename
     * @return
     */
    private String generateFileName(String originalFilename) {
        List<String> sufList = Arrays.stream(originalFilename.split("\\.")).collect(Collectors.toList());
        String suf = sufList.get(sufList.size() - 1);
        String pre = TimeUtil.getSecondsTimeStampStr();
        return pre + "." + suf;
    }

注意:docker部署区别,因为docker不能直接访问服务器目录,所以启动镜像时需指定挂载目录,不然文件会创建在容器目录下。当时就因为这个原因一直没上传到指定的目录

如上所示,该图片上传到该路径:/usr/local/nginx/upload/

上传完成后查看该路径下是否存在图片

获取服务器图片:

1:配置nginx

进入nginx.conf文件配置映射

server {

//映射端口号

listen 8086;

server_name localhost;

location / {

root upload;(该地址表示:/usr/local/nginx/upload)

index index.html index.htm;

}

}

2、重启nginx

3、获取图片:网址输入:IP+配置端口(8086)+文件名。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值