swagger图片上传

controller类:

package com.xinjian.znxj.modules.webapi.V1;

import com.xinjian.znxj.common.entity.R;
import com.xinjian.znxj.modules.image.entity.ZnxjPictureEntity;
import com.xinjian.znxj.modules.image.service.ZnxjPictureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

import static com.xinjian.znxj.common.utils.ShiroUtils.getUserId;
@RestController
@RequestMapping("/api/v1/images")
@Api(value = "图片接口", description = "图片接口")
public class ImageController {

    @Value("${app.ftp.base}")
    private  String basePath;
    @Autowired
    private ZnxjPictureService znxjPictureService;

    @PostMapping(value = "/upload",consumes = "multipart/*",headers = "content-type=multipart/form-date")
    @ApiOperation(value="文件上传", notes="文件上传")
    public R upload(@ApiParam(value = "上传的文件" ,required = true) MultipartFile file, HttpServletRequest request){

        long fileSize = file.getSize();
        Map<String,Object> map = new HashMap<>();
        if (file.isEmpty()) {
            return R.error(101,"文件为空");
        } else if (fileSize > (10 * 1024 * 1024)) {
            return R.error(101,"文件过大");
        } else {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            String oldFileName = fileName;
            // 获取文件的后缀名
            String extensionFileName = fileName.substring(fileName.lastIndexOf("."));
            Long userID = getUserId();
            //获取当前系统时间戳
            String dateStr=System.currentTimeMillis()+"";
            // 文件上传后的路径拼接
            String filePath=basePath+"/"+userID+"/"+dateStr+extensionFileName;
            // 上传文件
            File dest = new File(filePath);
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                file.transferTo(dest);
                String fileUrl="/img"+filePath.replace(basePath,"");
                String id = UUID.randomUUID().toString();
                ZnxjPictureEntity fileEntity = new ZnxjPictureEntity();
                fileEntity.setPictureid(id);
                fileEntity.setPictureaddr(fileUrl);
                fileEntity.setCreatedate(new Date());
                fileEntity.setCreateuser(getUserId()+"");
                fileEntity.setRemarks(oldFileName);
                fileEntity.setDelflag(0);
                R result=znxjPictureService.saveZnxjPicture(fileEntity);
                if("200".equals(String.valueOf(result.get("code")))){
                    map.put("id",id);
                    map.put("url",fileUrl);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return R.result(200,"success",map);
    }
}

swagger接口效果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值