图片上传后端

yml文件配置上传图片后的路径

#自行配置图片上传的路径
img:
  path: C:\Users\3234\Desktop\img\

controller层

import com.yjq.programmer.service.UploadService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/upload")
public class uploadController {

    @Resource
    private UploadService uploadService;
    @PostMapping("/uploadFile")
    public String uploadFile(MultipartFile file) throws IOException {
        return uploadService.uploadFile(file);
    }
}

Impl

package com.yjq.programmer.service.impl;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

public interface UploadServiceImpl {
    public String uploadFile(MultipartFile file) throws IOException;
}

Service

package com.yjq.programmer.service;

import com.yjq.programmer.service.impl.UploadServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class UploadService implements UploadServiceImpl {
    @Value("${img.path}")
    private String localPath;
    @Override
    public String uploadFile(MultipartFile file) throws IOException {
        //打印出文件名字
        System.out.println("file="+file);
        //上传头像本身的名字
        String originalFilename = file.getOriginalFilename();
        //根据  “.”  截取图片名字 后缀
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));

        //UUID截取七位数字给图片命名
        String name = UUID.randomUUID().toString().substring(1,8);

        //上传后,生成文件名写死的,固定的  ——  这样上传新的图片会覆盖掉原来的图片
        //file.transferTo(new File("C:/Users/3234/Desktop/img/hello.jpg"));

        File file1 = new File(localPath);
        //如果localPath配置文件路径文件夹不存在则新建一个
        if(!file1.exists()){
            file1.mkdirs();
        }

        file.transferTo(new File(localPath + name + substring));//生成新的文件路径 + 文件名 + 后缀名
        return name + substring;
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在uniapp中上图片后端,可以使用uni.uploadFile()方法。具体步骤如下: 1. 在前端页面中,使用uni.chooseImage()方法选择要上图片,并将选择的图片转换为base64格式或者临时文件路径。 2. 调用uni.uploadFile()方法,将图片后端服务器。在上时,需要设置url、filePath、name、header等参数。其中,url为后端接口地址,filePath为选择的图片路径,name为后端接口中接收图片的参数名,header为请求头信息。 3. 在后端接口中,接收前端递的图片,并进行处理。 示例代码如下: ``` // 前端页面中 uni.chooseImage({ success: function (res) { // 将选择的图片转换为base64格式或者临时文件路径 let tempFilePaths = res.tempFilePaths; uni.uploadFile({ url: 'http://example.com/upload', filePath: tempFilePaths[0], name: 'file', header: { 'content-type': 'multipart/form-data' }, success: function (res) { console.log(res.data); } }); } }); // 后端接口中 router.post('/upload', async (ctx, next) => { const file = ctx.request.files.file; // 获取上文件 const reader = fs.createReadStream(file.path); // 创建可读流 const ext = file.name.split('.').pop(); // 获取上文件扩展名 const fileName = `${uuidv4()}.${ext}`; // 生成新的文件名 const filePath = path.join(__dirname, '../public/uploads', fileName); // 生成新的文件路径 const upStream = fs.createWriteStream(filePath); // 创建可写流 reader.pipe(upStream); // 可读流通过管道写入可写流 ctx.body = { code: 200, message: '上成功', data: { url: `http://example.com/uploads/${fileName}` // 返回上后的文件路径 } }; }); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值