美文网首页
后台接收前端/客户端图片,并保存到电脑本地盘

后台接收前端/客户端图片,并保存到电脑本地盘

作者: 墨色尘埃 | 来源:发表于2017-10-09 16:29 被阅读0次

destDirName值从application.yml中设置

    @Value("${destDirName}")
    protected String destDirName;
.......

    /**
     * 网点上报
     * 其中request包含key为“object”的Parameter,值为branchPoint对象的序列化
     */
    @RequestMapping(value = "/branchPoint", method = RequestMethod.POST)
    public ResponseObj<Boolean> reportBranch(HttpServletRequest request) {

        if (request instanceof MultipartHttpServletRequest) {
            MultipartHttpServletRequest mulRequest = (MultipartHttpServletRequest) request;
            Set<Map.Entry<String, MultipartFile>> set = mulRequest.getFileMap().entrySet();
            String branchStr = mulRequest.getParameter("object");
            BranchPoint branchPoint;
            try {
                branchPoint = omapper.readValue(branchStr, BranchPoint.class);
            } catch (IOException e) {
                return new ResponseObj(false, new Error("数据格式不正确"));
            }
            //网点名称唯一
//            BranchPoint sameBranchPoint = companyMapper.getSmaneBranchPointName(branchPoint.getBranchName());
//            if (null != sameBranchPoint) {
//                if (!isEmpty(sameBranchPoint.getBranchName()) &&
//                        branchPoint.getBranchName().equals(sameBranchPoint.getBranchName())) {
//                    return new ResponseObj(false, new Error("网点名称" + "已经存在"));
//                }
//            }

            String photoId = UUID.randomUUID().toString();
            branchPoint.setPhoto(photoId);
            branchPoint.setCreateTime(getNowTime());
            companyMapper.insertBranchPoint(branchPoint);
            for (Map.Entry<String, MultipartFile> each : set) {
                String name = each.getValue().getOriginalFilename();
                String suffixal = getSuffixal(name);
                try {
                    writeToLocal(photoId + suffixal, each.getValue().getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                    return new ResponseObj(false, new Error("文件保存失败"));
                }
            }
        }
        return new ResponseObj(true, null);

    }
/**
     * 将InputStream写入本地文件
     * 输入流
     *
     * @throws IOException
     */

    private void writeToLocal(String fileName, InputStream input)
            throws IOException {
        createDir(destDirName);
        String dirFile = destDirName + "/" + fileName;
        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(dirFile);
        while ((index = input.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        downloadFile.close();
        input.close();
    }
/**
     * 创建文件夹
     *
     * @param destDirName
     * @return
     */
    public boolean createDir(String destDirName) {
        File dir = new File(destDirName);
        if (dir.exists()) {
            return false;
        }
        if (!destDirName.endsWith(File.separator)) {
            destDirName = destDirName + File.separator;
        }
        //创建目录
        if (dir.mkdirs()) {

            return true;
        } else {

            return false;
        }
    }
/**
     * 截取图片后缀名
     */
    public String getSuffixal(String name) {
        String str = name.substring(name.length() - 4, name.length());
        return str;
    }

application.yml

mybatis:
    mapperLocations: classpath:com/jspptd/postal/collectserver/mapping/*.xml
    typeAliasesPackage: com.jspptd.postal.collectserver.model
server:
  port: 10002

#    interfaceUrl:
#      ipUrl: http://132.228.226.11
#    #  ftpUrl: http://132.228.226.11
#    #  ftpUrl: http://172.16.214.24
#      ftpUrl: http://172.16.108.137  # 本机的ip
#      oaUrl: http://nt.jsoa.net
#      vedioUrl: http://58.223.251.5:8080/axis2/services/GEForMsp

spring:
  resource: 
    static-locations: file:/public/
  http:
    multipart:
      max-file-size: 20Mb
      max-request-size: 80Mb
  datasource:
    url: jdbc:mysql://172.16.12.196:3306/postial?autoReconnect=true
#    jdbc:oracle:thin:@172.16.241.177:1521:rjyorcl
    username: root
    password: jsptpd196
#    url: jdbc:oracle:thin:@132.228.226.11:1521:JSEIPDB
#    username: newsec
#    password: passw0rd
    driver-class-name: com.mysql.jdbc.Driver
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    #自动检测关闭和空闲连接的间隔
    timeBetweenEvictionRunsMillis: 30000
    #最小生存时间
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    #这个参数设置为true,可以保证每次的连接都是正常的,但是。。 性能会降低, 建议使用空闲下检测30s一次, 这样服务器最多30s出现问题后恢复
    testOnBorrow: false
    testOnReturn: false
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=3000
    filters: stat
    #PSCatch
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
destDirName: picture/

相关文章

网友评论

      本文标题:后台接收前端/客户端图片,并保存到电脑本地盘

      本文链接:https://www.haomeiwen.com/subject/aijpyxtx.html