当前位置:   article > 正文

Day22_appendfilestorageclient is null

appendfilestorageclient is null
  1. 关于图片文件的上传与回显
    SpringMVC的MultipartFile类实现文件的上传
    (1)上传
    domain:File
import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.enums.IdType;

import java.io.Serializable;
import java.util.Date;


public class File extends Model<File> {


    private static final long serialVersionUID = 1L;
	@TableId(value="id", type= IdType.UUID)
	private String id;
	private String name;
	private String url;
	private String remarks;
	private Date createDate;
	private String createUser;
	private String suffix;
	private String fileMd5;
	private String groupName;
	private String fullPath;
	private String size;
	private String brandOfficeId;

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public String getBrandOfficeId() {
		return brandOfficeId;
	}

	public void setBrandOfficeId(String brandOfficeId) {
		this.brandOfficeId = brandOfficeId;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getFullPath() {
		return fullPath;
	}

	public void setFullPath(String fullPath) {
		this.fullPath = fullPath;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}


	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getFileMd5() {
		return fileMd5;
	}

	public void setFileMd5(String fileMd5) {
		this.fileMd5 = fileMd5;
	}

	@Override
	protected Serializable pkVal() {
		return this.id;
	}


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131

model:FileDto

import java.util.Date;

public class FileDto {


    private static final long serialVersionUID = 1L;
	private String id;
	private String name;
	private String url;
	private String remarks;
	private Date createDate;
	private String createUser;
	private String suffix;
	private String fileMd5;
	private String groupName;
	private String fullPath;
	private String size;
	private String brandOfficeId;

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public String getBrandOfficeId() {
		return brandOfficeId;
	}

	public void setBrandOfficeId(String brandOfficeId) {
		this.brandOfficeId = brandOfficeId;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getFullPath() {
		return fullPath;
	}

	public void setFullPath(String fullPath) {
		this.fullPath = fullPath;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}


	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getFileMd5() {
		return fileMd5;
	}

	public void setFileMd5(String fileMd5) {
		this.fileMd5 = fileMd5;
	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119

Controller:FileController

package com.kcidea.bms.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.tobato.fastdfs.service.AppendFileStorageClient;
import com.kcidea.bms.common.bean.ObjectResult;
import com.kcidea.bms.common.util.*;
import com.kcidea.bms.common.utils.ReadProper;
import com.kcidea.bms.domain.User;
import com.kcidea.bms.model.FileDto;
import com.kcidea.bms.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * Created by Administrator on 2019/3/27 0027.
 */
@Slf4j
@Controller
@RequestMapping("/file")
public class FileController {

    @Resource
    FileService fileService;

    @Resource
    private AppendFileStorageClient appendFileStorageClient;

    @Resource
    private FastDFSClientWrapper fastDFSClientWrapper;

    /**
     * 文件上传
     * @param file
     * @param redirectAttributes
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public ObjectResult singleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes,String type, HttpSession session) {
        ObjectResult result = new ObjectResult();
        result.setFlag(false);
        if (file.isEmpty()) {
            result.setMsg(Vm.DAMAGED_OR_EMPTY_FILES);
            return result;
        }
        try {
            FileDto fileDto=saveFile(file);
            User user = WebUtils.getUserBySession(session);
            fileDto.setCreateDate(new Date());
            fileDto.setCreateUser(user.getId());
            fileDto.setBrandOfficeId(user.getBrandOfficeId());
            com.kcidea.bms.domain.File tempFile = fileService.addFile(fileDto);
            result.setFlag(true);
            result.setData(tempFile);
            result.setMsg(Vm.SUCCESS);
            return result;
        } catch (Exception e) {
            log.error("文件上传失败:"+e.getMessage(),e);
        }
        return result;
    }

    /**
     * 保存文件
     * @param multipartFile
     * @return
     * @throws IOException
     */
    public FileDto saveFile(MultipartFile multipartFile) throws IOException {
        FileDto fileDto = new FileDto();
        String[] fileAbsolutePath={};
        String fileName=multipartFile.getOriginalFilename();

        fileDto.setName(fileName.substring(0,fileName.lastIndexOf(".")));

        String ext = fileName.substring(fileName.lastIndexOf(".") + 1);

        fileDto.setSuffix(ext);

        byte[] file_buff = null;
        InputStream inputStream=multipartFile.getInputStream();
        if(inputStream!=null){
            int len1 = inputStream.available();
            file_buff = new byte[len1];
            inputStream.read(file_buff);
        }
        inputStream.close();
        FastDFSFile file = new FastDFSFile(fileName, file_buff, ext);
        try {
            fileAbsolutePath = FastDFSClient.upload(file);  //upload to fastdfs
        } catch (Exception e) {
            log.error("文件上传失败:"+e.getMessage(),e);
        }
        if (fileAbsolutePath==null) {
            log.error("upload file failed,please upload again!");
        }
        String path=FastDFSClient.getTrackerUrl()+fileAbsolutePath[0]+ "/"+fileAbsolutePath[1];
        fileDto.setUrl(fileAbsolutePath[1]);
        fileDto.setGroupName(fileAbsolutePath[0]);
        fileDto.setFullPath(path);
        return fileDto;
    }


    /**
     * 文件删除接口
     * @param response
     * @param groupName
     * @param remoteFileName
     * @throws IOException
     */
    @RequestMapping(value = "/deleteFile")
    @ResponseBody
    public ObjectResult deleteFile(HttpServletResponse response,String groupName,String remoteFileName,String fileId) throws IOException {
        ObjectResult result = new ObjectResult();
        try {
            //例子FastDFSClient.deleteFile("group1","M00/00/00/wKgBrVycZQ-ARi-9AAIMpuR-Zfc06.jpeg");
            FastDFSClient.deleteFile(groupName,remoteFileName);
            /*projectFileRelService.deleteProjectFileRelByFileId(fileId);*/
            result.setFlag(true);

        } catch (IOException e) {
            log.error("文件删除失败:"+e.getMessage(),e);
            result.setFlag(false);
            return result;
        } catch (Exception e) {
            log.error("文件删除失败:"+e.getMessage(),e);
            result.setFlag(false);
            return result;
        }
        return result;
    }

    /**
     * 根据md5查询文件是否存在
     * @param md5
     * @return
     */
    @RequestMapping(value = "/getFileByMd5")
    @ResponseBody
    public ObjectResult getFileByMd5(String md5,HttpSession session){
        ObjectResult result = new ObjectResult();
        result.setFlag(false);
        User user = WebUtils.getUserBySession(session);
        if(null==user){
            //未登录
            result.setMsg(Vm.USER_NOT_LOGIN);
            return result;
        }
        if(StringUtils.isBlank(md5)){
            result.setMsg(Vm.REQUIRED_ENTRIES_ARE_EMPTY);
            return result;
        }
        try {
            com.kcidea.bms.domain.File file = fileService.getFileByMd5(md5);
            result.setData(file);
            result.setMsg(Vm.SUCCESS);
            result.setFlag(true);
        }catch (Exception e){
            result.getMsg(Vm.FAIL);
            log.error("根据md5查询文件是否存在:"+e.getMessage(),e);
        }
        return result;
    }


    //获取配置
    @RequestMapping("/config")
    @ResponseBody
    public String config(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("fileServerUrl", "http:\\\\"+ ReadProper.getResourceValue("fileServerUrl")+"/");
        jsonObject.put("maxSize", ReadProper.getResourceValue("maxSize"));
        return jsonObject.toString();
    }


    /**
     *  文件上传
     *  最后返回fastDFS中的文件名称;group1/M00/01/04/CgMKrVvS0geAQ0pzAACAAJxmBeM793.doc
     * @param file 页面提交时文件
     * @return
     */
    @PostMapping("/uploadFile")
    public String uploadFile(MultipartFile file){
        byte[] bytes = new byte[0];
        try {
            bytes = file.getBytes();
        } catch (IOException e) {
            log.error("获取文件错误");
            e.printStackTrace();
        }
        //获取源文件名称
        String originalFileName = file.getOriginalFilename();
        //获取文件后缀--.doc
        String extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
        String fileName = file.getName();
        //获取文件大小
        long fileSize = file.getSize();
        System.out.println(originalFileName + "==" + fileName + "==" + fileSize + "==" + extension + "==" + bytes.length);
        return fastDFSClientWrapper.uploadFile(bytes, fileSize, extension);
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220

FileService

package com.kcidea.bms.service;

import com.kcidea.bms.domain.File;
import com.kcidea.bms.model.FileDto;

/**
 * Created by kcidea-code on 2019/3/29.
 */
public interface FileService {
    /**
     * 添加一个文件
     * @param fileDto
     */
    File addFile(FileDto fileDto);


    /**
     * 文件id查询文件
     * @param ysbgFileId
     * @return
     */
    File getFileById(String ysbgFileId);

    /**
     * md5 查询文件信息
     * @param md5
     * @return
     */
    File getFileByMd5(String md5);

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

FileServiceImpl

package com.kcidea.bms.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kcidea.bms.common.util.Contants;
import com.kcidea.bms.common.util.MsgException;
import com.kcidea.bms.common.util.Vm;
import com.kcidea.bms.dao.*;
import com.kcidea.bms.domain.File;
import com.kcidea.bms.model.*;
import com.kcidea.bms.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kcidea-code on 2019/3/29.
 */
@Service
@Slf4j
@Transactional
public class FileServiceImpl implements FileService{

    @Resource
    private FileDao fileDao;

    @Override
    public File addFile(FileDto fileDto) {
        File file = new File();
        BeanUtils.copyProperties(fileDto,file);
        fileDao.insert(file);
        return file;
    }

    @Override
    public File getFileById(String ysbgFileId) {
        return fileDao.selectById(ysbgFileId);
    }

    @Override
    public File getFileByMd5(String md5) {
        File file = new File();
        file.setFileMd5(md5);
        file = fileDao.selectOne(file);
        return file;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

FileDao

package com.kcidea.bms.dao;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.kcidea.bms.domain.File;
import com.kcidea.bms.model.FileDto;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by kcidea-code on 2019/3/29.
 */
@Repository
public interface FileDao extends BaseMapper<File>{
    /**
     * 查询近一个月的文件
     * @return
     */
    List<FileDto> findAllFilesByLastMonth();

    /**
     * 根据项目id查询当前项目中的文件
     * @param projectId
     * @return
     */
    List<FileDto> findFilesByProjectId(String projectId);
    /**
     * @Description 根据项目id查询当前项目中的文件/分页
     * @Author wuliwei
     * @Date 2019/5/6
     * @Param [projectId, page]
     * @return java.util.List<com.kcidea.architecture.model.FileDto>
     */
    List<FileDto> findFilesByProjectIdPage(@Param("projectId") String projectId, @Param("page") Page<FileDto> page, @Param("fileName") String fileName);

    /**
     *
     * @param visaId
     * @param fileType
     * @return
     */
    List<FileDto> findFilesByVisaIdAndFileType(@Param("visaId") String visaId, @Param("fileType") String fileType);

    /**
     *
     * @param progressId
     * @return
     */
    List<FileDto> findFilesByProgressId(@Param("progressId") String progressId, @Param("type") String type);

    /**
     * 根据预算查询文件
     * @param budgetId
     * @param page
     * @param fileName
     * @param currentStep
     * @return
     */
    List<FileDto> findFilesByBudgetIdPage(@Param("budgetId") String budgetId, @Param("page") Page<FileDto> page, @Param("fileName") String fileName, @Param("currentStep") String currentStep);

    /**
     * 根据结算查询文件
     * @param settlementId
     * @param page
     * @param fileName
     * @param currentStep
     * @return
     */
    List<FileDto> findFilesBySettlementIdPage(@Param("settlementId") String settlementId, @Param("page") Page<FileDto> page, @Param("fileName") String fileName, @Param("currentStep") String currentStep);

    /**
     * 根据文件id查询文件信息
     * @param fileId
     * @return
     */
    FileDto getFileById(@Param("fileId") String fileId);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  1. @TableId:用于对实体类的主键进行注解,标记为主键。【Mybatis-plus全局配置主键生成策略】
    @Table(value=“xx”,type=xxx )
    其中Type有六种,如下图。
    在这里插入图片描述

  2. @Override
    protected Serializable pkVal(){return this.id}//用于支持组合主键,一般在实体类继承Model类后会用到。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/170177
推荐阅读
相关标签
  

闽ICP备14008679号