第十四节 附件上传

1 附件上传

1.1controller

package com.yinghui.soft.web;

import com.alibaba.fastjson.JSONArray;
import com.yinghui.soft.model.TAppendix;
import com.yinghui.soft.model.TXlqImage;
import com.yinghui.soft.service.TAppendixService;
import com.yinghui.soft.utils.LogAnnotation;
import com.yinghui.soft.utils.PJCommon;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.yinghui.soft.web.BaseController.getParametersUserinfo;
import static com.yinghui.soft.web.BaseController.logger;

@RequestMapping("/TAppendixController")
@RestController
public class TAppendixController {
    @Autowired
    private TAppendixService service;

    @Value("${tomcatPath}")
    String tomcatPath;
    @Value("${ipPort}")
    String ipPort;
    @Value("${tomcatUrl}")
    String tomcatUrl;

    @Value("${fileTag}")
    String fileTag;
    @Value("${nextFile}")
    String nextFile;

    
    @LogAnnotation(firstLevel = "数据管理", secondLevel = "附件", noteLevel = "新增")
    @PostMapping(value = "pCSaveFile")
    public Map saveFile(HttpServletRequest request, @RequestParam("url") MultipartFile file) {
        Map map = new HashMap(4);
        try{
            Map  maps= getParametersUserinfo(request);
            Integer xmId = Integer.parseInt(maps.get("xmId").toString());
            Integer type = Integer.parseInt(maps.get("type").toString());
            String userid=maps.get("userid").toString();

            //定义文件上传地址
            String url="";
            String infoUrl="";
            String fileName = file.getOriginalFilename();
            String subStr =  fileName.substring(fileName.lastIndexOf("."),fileName.length());
            String data= new SimpleDateFormat("yyyyMMdd").format(new Date());
            String newFileName=  new Date().getTime()+"";
            try{
                url=tomcatPath+ File.separator+fileTag+ File.separator+nextFile+ File.separator+data+"\\";
                File f = new File(url);
                if(!f.exists()){
                    f.mkdirs();
                }
                String urlWj=tomcatPath+ File.separator+fileTag+ File.separator+nextFile+ File.separator+data+File.separator+newFileName+subStr;

                //  输入流
                InputStream is = file.getInputStream();
                //创建一个文件输出流
                FileOutputStream fos = new FileOutputStream(urlWj);
                //创建一个缓冲区
                byte buffer[] = new byte[1024];
                //判断输入流中的数据是否已经读完的标识
                int length = 0;
                //循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
                while((length = is.read(buffer))>0){
                    //使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
                    fos.write(buffer, 0, length);
                }
                //关闭输入流
                is.close();
                //关闭输出流
                fos.close();
                infoUrl="/"+fileTag+"/"+nextFile+"/"+data+"/"+newFileName+subStr;

            }catch (Exception e){
                e.printStackTrace();
                map.put("flag",false);
                map.put("msg","文件上传出错");
                return map;
            }
            TAppendix TAppendix=new TAppendix();
            TAppendix.setXmId(xmId);
            TAppendix.setType(type);
            TAppendix.setPath(infoUrl);

            if (maps.containsKey("remarks")
                    &&!StringUtils.isEmpty(maps.get("remarks"))) {
                String remarks = maps.get("remarks").toString();
                TAppendix.setRemarks(remarks);
            }
            TAppendix.setCreateUser(Integer.parseInt(userid));
            TAppendix.setCreateTime(new Date());
            int  i =service.save(TAppendix);
            if (i == 1) {
                map.put("flag", true);
                map.put("msg", "保存成功");
            } else {
                map.put("flag", false);
                map.put("msg", "保存失败");
            }
        } catch (Exception e) {
            logger.error("参数错误", e);
            map.put("flag", false);
            map.put("msg", "参数错误");
        }
        return map;
    }
}

1.2 appliction.yml 

ipPort: http://localhost:8080
tomcatPath: D:/Tomcat-9.0.37/webapps
tomcatUrl: D:/Tomcat-9.0.37/webapps/hjxzxmdc
time: 12000000
fileTag: hjxzxmdc
nextFile: ak_bh_xmgc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

akglobe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值