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
![](https://i-blog.csdnimg.cn/blog_migrate/48d444e8f4a30600084e3c5e1e1bb5e4.png)