作者 朱兆平

文件上传及入库

@@ -7,6 +7,8 @@ readDirectory = /Users/mrz/Documents/java项目/test @@ -7,6 +7,8 @@ readDirectory = /Users/mrz/Documents/java项目/test
7 #接收存储报文目录 7 #接收存储报文目录
8 bakDirectory = kakoRevice 8 bakDirectory = kakoRevice
9 #是否需要发送报文,默认N不发,Y将发送readDirectory下的XML扩展名的报文 9 #是否需要发送报文,默认N不发,Y将发送readDirectory下的XML扩展名的报文
  10 +#上传文件路径,必填
  11 +uploadDirectory = upload
10 isNeedSend = Y 12 isNeedSend = Y
11 13
12 #IMF MEAT报头配置 14 #IMF MEAT报头配置
  1 +package com.tianbo.warehouse.controller;
  2 +
  3 +import com.tianbo.warehouse.controller.response.ResultJson;
  4 +import com.tianbo.warehouse.model.Attachment;
  5 +import com.tianbo.warehouse.service.AttachmentService;
  6 +import com.tianbo.warehouse.util.Date.DateUtil;
  7 +import com.tianbo.warehouse.util.Helper;
  8 +import org.apache.commons.io.FileUtils;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.web.bind.annotation.PostMapping;
  11 +import org.springframework.web.bind.annotation.RequestParam;
  12 +import org.springframework.web.bind.annotation.RestController;
  13 +import org.springframework.web.multipart.MultipartFile;
  14 +
  15 +import java.io.File;
  16 +import java.io.IOException;
  17 +
  18 +import static com.tianbo.warehouse.util.IO.FileTool.readProperties;
  19 +
  20 +@RestController
  21 +public class UploadController {
  22 +
  23 + @Autowired
  24 + private AttachmentService attachmentService;
  25 +
  26 + /**
  27 + * 单个文件上传
  28 + * @param file
  29 + * @return
  30 + * @throws Exception
  31 + */
  32 + @PostMapping("/upload")
  33 + public ResultJson upload(@RequestParam MultipartFile file) throws Exception {
  34 + ResultJson resultJson = new ResultJson();
  35 + if (null!=file && !file.isEmpty() && file.getSize()>0){
  36 + byte[] filebyte = file.getBytes();
  37 + String fileLitleName = file.getName();
  38 + String fileName = file.getOriginalFilename();
  39 + String fileType = fileName.substring((fileName.lastIndexOf(".")));
  40 + String saveDir = readProperties("uploadDirectory");
  41 + StringBuffer stringBuffer = new StringBuffer();
  42 + String saveFileName = stringBuffer.append(saveDir).append("/").append(DateUtil.getToday()).append("/").append(Helper.getUUID()).append(fileType).toString();
  43 + File saveFile = new File(saveFileName);
  44 + if(!saveFile.getParentFile().exists()){
  45 + saveFile.mkdirs();
  46 + }
  47 +
  48 + try{
  49 + FileUtils.writeByteArrayToFile(saveFile,filebyte);
  50 + Attachment attachment = new Attachment(saveFileName);
  51 + int i =attachmentService.insertSelective(attachment);
  52 + resultJson = (i==1 ? new ResultJson("200","附件入库成功",saveFileName) :new ResultJson("500","附件入库失败"));
  53 + }catch (IOException e){
  54 + resultJson = new ResultJson("400","附件存储失败",e);
  55 + e.printStackTrace();
  56 + }catch (IllegalStateException e){
  57 + resultJson = new ResultJson("400","附件存储失败",e);
  58 + e.printStackTrace();
  59 + }
  60 + }
  61 + return resultJson;
  62 + }
  63 +
  64 + /**
  65 + * 批量上传
  66 + * @param files
  67 + * @return
  68 + * @throws Exception
  69 + */
  70 + @PostMapping("/uploads")
  71 + public ResultJson upload(MultipartFile[] files) throws Exception {
  72 + if (files!=null && files.length>0){
  73 + // ... 处理文件储存逻辑
  74 + for (MultipartFile file : files) {
  75 + if (!file.isEmpty() && file.getSize()>0){
  76 + String fileName = file.getName();
  77 + String fileType = file.getContentType();
  78 + String saveDir = readProperties("uploadDirectory");
  79 + StringBuffer stringBuffer = new StringBuffer();
  80 + stringBuffer.append(saveDir).append("/").append(DateUtil.getToday()).append("/").append(fileType).append(Helper.getUUID()).append(fileName);
  81 + File saveFile = new File(stringBuffer.toString());
  82 + try{
  83 + file.transferTo(saveFile);
  84 + }catch (IOException e){
  85 + e.printStackTrace();
  86 + }catch (IllegalStateException e){
  87 + e.printStackTrace();
  88 + }
  89 +
  90 + }
  91 +
  92 + }
  93 + }
  94 + return new ResultJson();
  95 + }
  96 +}
  1 +package com.tianbo.warehouse.dao;
  2 +
  3 +import com.tianbo.warehouse.model.Attachment;
  4 +
  5 +public interface AttachmentMapper {
  6 + int deleteByPrimaryKey(Integer aid);
  7 +
  8 + int insert(Attachment record);
  9 +
  10 + int insertSelective(Attachment record);
  11 +
  12 + Attachment selectByPrimaryKey(Integer aid);
  13 +
  14 + int updateByPrimaryKeySelective(Attachment record);
  15 +
  16 + int updateByPrimaryKey(Attachment record);
  17 +}
  1 +package com.tianbo.warehouse.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +@Data
  6 +public class Attachment {
  7 + private Integer aid;
  8 +
  9 + private String path;
  10 +
  11 + public Attachment(String path){
  12 + this.path = path;
  13 + }
  14 +}
  1 +package com.tianbo.warehouse.service;
  2 +
  3 +import com.tianbo.warehouse.model.Attachment;
  4 +
  5 +public interface AttachmentService {
  6 +
  7 + int insertSelective(Attachment record);
  8 +}
  1 +package com.tianbo.warehouse.service.imp;
  2 +
  3 +import com.tianbo.warehouse.dao.AttachmentMapper;
  4 +import com.tianbo.warehouse.model.Attachment;
  5 +import com.tianbo.warehouse.service.AttachmentService;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.stereotype.Service;
  8 +
  9 +@Service
  10 +public class AttachmentServiceImp implements AttachmentService {
  11 +
  12 + @Autowired
  13 + private AttachmentMapper attachmentMapper;
  14 +
  15 + @Override
  16 + public int insertSelective(Attachment record){
  17 + return attachmentMapper.insertSelective(record);
  18 + }
  19 +}
@@ -45,6 +45,6 @@ @@ -45,6 +45,6 @@
45 <property name="enableSubPackages" value="true"/> 45 <property name="enableSubPackages" value="true"/>
46 </javaClientGenerator> 46 </javaClientGenerator>
47 <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> 47 <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
48 - <table tableName="log" domainObjectName="LOG" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> 48 + <table tableName="attachment" domainObjectName="Attachment" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
49 </context> 49 </context>
50 </generatorConfiguration> 50 </generatorConfiguration>
  1 +<?xml version="1.0" encoding="UTF-8" ?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3 +<mapper namespace="com.tianbo.warehouse.dao.AttachmentMapper" >
  4 + <resultMap id="BaseResultMap" type="com.tianbo.warehouse.model.Attachment" >
  5 + <id column="aid" property="aid" jdbcType="INTEGER" />
  6 + <result column="path" property="path" jdbcType="VARCHAR" />
  7 + </resultMap>
  8 + <sql id="Base_Column_List" >
  9 + aid, path
  10 + </sql>
  11 + <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  12 + select
  13 + <include refid="Base_Column_List" />
  14 + from attachment
  15 + where aid = #{aid,jdbcType=INTEGER}
  16 + </select>
  17 + <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
  18 + delete from attachment
  19 + where aid = #{aid,jdbcType=INTEGER}
  20 + </delete>
  21 + <insert id="insert" parameterType="com.tianbo.warehouse.model.Attachment" >
  22 + insert into attachment (aid, path)
  23 + values (#{aid,jdbcType=INTEGER}, #{path,jdbcType=VARCHAR})
  24 + </insert>
  25 + <insert id="insertSelective" parameterType="com.tianbo.warehouse.model.Attachment" >
  26 + insert into attachment
  27 + <trim prefix="(" suffix=")" suffixOverrides="," >
  28 + <if test="aid != null" >
  29 + aid,
  30 + </if>
  31 + <if test="path != null" >
  32 + path,
  33 + </if>
  34 + </trim>
  35 + <trim prefix="values (" suffix=")" suffixOverrides="," >
  36 + <if test="aid != null" >
  37 + #{aid,jdbcType=INTEGER},
  38 + </if>
  39 + <if test="path != null" >
  40 + #{path,jdbcType=VARCHAR},
  41 + </if>
  42 + </trim>
  43 + </insert>
  44 + <update id="updateByPrimaryKeySelective" parameterType="com.tianbo.warehouse.model.Attachment" >
  45 + update attachment
  46 + <set >
  47 + <if test="path != null" >
  48 + path = #{path,jdbcType=VARCHAR},
  49 + </if>
  50 + </set>
  51 + where aid = #{aid,jdbcType=INTEGER}
  52 + </update>
  53 + <update id="updateByPrimaryKey" parameterType="com.tianbo.warehouse.model.Attachment" >
  54 + update attachment
  55 + set path = #{path,jdbcType=VARCHAR}
  56 + where aid = #{aid,jdbcType=INTEGER}
  57 + </update>
  58 +</mapper>