zoukankan      html  css  js  c++  java
  • java springboot -- MultipartFile -图片上传到远程服务器上

    /***
    * 新增文件
    * @param file
    * @return
    */
    private boolean saveFile(MultipartFile file) {

    try {
    // 文件保存路径
    String filePath = "A://merchant//"; //映射的地址
    //String filePath = request.getSession().getServletContext().getRealPath("upload/");本地项目路径

    String filename = file.getOriginalFilename();//获取file图片名称

    uploadFile(file.getBytes(), filePath, filename);


    PictureRes picRes = new PictureRes(); //数据库中只存图片表
    picRes.setIsType(isType);//图片类型
    picRes.setPicPath("http://192.168.1.....:8080/upload/merchant/"+filename);//远程tomcat下的路径+图片名;保存在数据库中,
    picDao.save(picRes);
    return true;

    } catch (Exception e) {
    e.printStackTrace();
    }
    return false;
    }

    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
    File targetFile = new File(filePath);
    if(!targetFile.exists()){
    targetFile.mkdirs();
    }
    FileOutputStream out = new FileOutputStream(filePath+fileName);
    out.write(file);
    out.flush();
    out.close();
    }

    以下图片是本地进行映射网络;在此之前需要在tomcat服务器上面进行共享

     tomcat服务器上面进行共享文件

     

  • 相关阅读:
    站立会议(二)
    站立会议(一)
    买书优惠问题
    软件的NABCD----安装部分
    你的灯亮着吗读书笔记(一)
    软件工程概论---环状二维数组最大子数组和
    梦断代码读书笔记(三)
    梦断代码读书笔记(二)
    课程作业3.10
    软件工程作业提交3.06
  • 原文地址:https://www.cnblogs.com/zhengjie-yjx/p/8032464.html
Copyright © 2011-2022 走看看