elementplus+springboot实现图片上传

本文介绍了Java中的FileUtil类,用于处理图片上传,包括上传文件到服务器路径以及前端通过El-Upload组件进行上传。后端通过@Controller的uploadImage方法处理上传,允许静态资源访问。
摘要由CSDN通过智能技术生成
public class FileUtil {
    public static  void uploadFile(byte[] file,String filePath,String fileName) throws IOException {
        File targetFile = new File(filePath);
        if (!targetFile.exists()){
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(targetFile.getAbsolutePath() + "/" + fileName);
        out.write(file);
        out.flush();
        out.close();
    }
    public static String getUpLoadFilePath(){
        File path = null;
        try {
            path=new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (!path.exists()) path =new File("");
        File filePath=new File(path.getAbsolutePath(),"static/images/upload/");
        return filePath.getAbsolutePath();
    }
}

 上面是fileUtil包用来实现图片下载

前端代码如下

<el-form-item label="图片上传">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:8080/file/upload"
              :show-file-list="false"
              :on-success="handleSuccess"

          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar"  alt="asd"/>
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>

前端数据定义

 imageUrl:"",//图片地址

 组件注册和成功方法

components:{
   Plus
  },
  methods: {
    handleSuccess(res){
      console.log(res)
      console.log(res.data)
      this.imageUrl="http://localhost:8080/images/upload/"+res.data
    },

后端允许静态访问

 @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }

controller方法

public class UploadController {
    @Resource
    UploadService uploadService;
    @PostMapping("/upload")
    Result<String> uploadImage(MultipartFile file){
        String oldFilename = file.getOriginalFilename();
        String filePath = FileUtil.getUpLoadFilePath();
        String newFilename = System.currentTimeMillis() + oldFilename;
        try {
            FileUtil.uploadFile(file.getBytes(),filePath,newFilename);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return new Result<>(State.SUCCESS,newFilename);
    }

}

实现无限级侧边栏可以使用 Vue Router 中的嵌套路由,以及Element Plus中的菜单组件。具体实现步骤如下: 1. 在 Vue Router 中配置嵌套路由,例如: ```js const routes = [ { path: '/', component: Home, children: [ { path: 'about', component: About }, { path: 'products', component: Products, children: [ { path: 'category/:categoryId', component: Category } ] } ] } ] ``` 其中,`children` 表示该路由的子路由,可以无限嵌套下去。 2. 在侧边栏中使用 Element Plus 的菜单组件,并使用 `router-link` 标签指定路由。例如: ```html <el-menu-item index="/about"> <i class="el-icon-menu"></i> <span slot="title">关于我们</span> </el-menu-item> <el-submenu index="/products"> <template slot="title"> <i class="el-icon-goods"></i> <span>产品列表</span> </template> <el-menu-item v-for="category in categories" :key="category.id" :index="'/products/category/' + category.id" > {{ category.name }} </el-menu-item> </el-submenu> ``` 其中,`el-menu-item` 和 `el-submenu` 分别表示菜单项和子菜单,`index` 属性指定对应的路由路径,`router-link` 标签会自动为其添加点击事件,点击后会跳转到对应的路由页面。 3. 在路由页面中使用嵌套的 `<router-view>` 标签来显示子路由的内容。例如: ```html <template> <div> <h1>产品列表</h1> <el-menu default-active="/products/category/1" class="el-menu-vertical-demo" router > <el-menu-item v-for="category in categories" :key="category.id" :index="'/products/category/' + category.id" > {{ category.name }} </el-menu-item> </el-menu> <router-view></router-view> </div> </template> ``` 其中,`default-active` 属性指定默认选中的菜单项,`router` 属性指定使用路由模式。 通过以上步骤,即可实现无限级侧边栏。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值