java110
2021-09-11 7ac92c5c1c386cc294ca0475a3bb7ecbd973f4dc
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
package com.java110.api.smo.file.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.java110.api.smo.DefaultAbstractComponentSMO;
import com.java110.core.component.BaseComponentSMO;
import com.java110.core.context.IPageData;
import com.java110.entity.component.ComponentValidateResult;
import com.java110.api.smo.file.IAddFileSMO;
import com.java110.utils.constant.PrivilegeCodeConstant;
import com.java110.utils.constant.ServiceConstant;
import com.java110.utils.util.Assert;
import com.java110.utils.util.Base64Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.io.InputStream;
 
/**
 * 添加小区服务实现类
 * add by wuxw 2019-06-30
 */
@Service("addFileSMOImpl")
public class AddFileSMOImpl extends DefaultAbstractComponentSMO implements IAddFileSMO {
 
    @Autowired
    private RestTemplate restTemplate;
 
 
 
    @Override
    public ResponseEntity<String> saveFile(IPageData pd, MultipartFile uploadFile) throws IOException {
 
        JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
        if (uploadFile.getSize() > 2 * 1024 * 1024) {
            throw new IllegalArgumentException("上传文件超过两兆");
        }
 
        Assert.hasKeyAndValue(paramIn, "communityId", "必填,请填写小区ID");
        Assert.hasKeyAndValue(paramIn, "suffix", "必填,请填写文件类型");
        super.checkUserHasPrivilege(pd, restTemplate, PrivilegeCodeConstant.SAVE_FILE);
 
        ComponentValidateResult result = this.validateStoreStaffCommunityRelationship(pd, restTemplate);
        InputStream is = uploadFile.getInputStream();
        String fileContext = Base64Convert.ioToBase64(is);
        paramIn.put("context", fileContext);
        paramIn.put("fileName", uploadFile.getOriginalFilename());
 
 
        String apiUrl = ServiceConstant.SERVICE_API_URL + "/api/file.saveFile" ;
 
 
        ResponseEntity<String> responseEntity = this.callCenterService(restTemplate, pd, paramIn.toJSONString(),
                apiUrl,
                HttpMethod.POST);
        return responseEntity;
 
    }
 
 
 
    public RestTemplate getRestTemplate() {
        return restTemplate;
    }
 
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
}