获取excel文件里数据。
public JSONObject qzccImportFile(@RequestParam("file") MultipartFile file) {
JSONObject jsonObject = new JSONObject();
try {
ExcelReader reader = ExcelUtil.getReader(file.getInputStream(), 0);
List<Map<String, Object>> mapList = reader.readAll();//拿到excel里面的数据形成list
............
word文件的下载。
public void down(
HttpServletRequest request, HttpServletResponse response) throws Exception {
String batch = request.getParameter("batch");
if (batch.equals("")) {
response.setStatus(500);
response.getWriter().append("server error");
return;
}
String sql = "select * from api_file where batch = ?";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, batch);
if (maps.size() <= 0) {
response.setStatus(500);
response.getWriter().append("server error");
return;
}
Map<String, Object> map = maps.get(0);
//String batch = (String) map.get("batch");
String name = (String) map.get("name");
String file = (String) map.get("file");
String type = (String) map.get("type");
if (file == null) {
response.setStatus(500);
response.getWriter().append("server error");
return;
}
try {
//解密file
BASE64Decoder decoder = new BASE64Decoder();
//前台在用Ajax传base64值的时候会把base64中的+换成空格,所以需要替换回来。
String baseValue = file.replaceAll(" ", "+");
//去除base64中无用的部分
byte[] bytes = decoder.decodeBuffer(baseValue.replace("data:image/png;base64,", ""));
OutputStream outputStream = response.getOutputStream();
response.setContentType("application/octet-stream;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(name + "." + type, "utf-8"));
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
response.setStatus(500);
response.getWriter().append("server error");
}
}
word文件上传。
public JSONObject uploadFile(@RequestParam String batch, @RequestParam MultipartFile file) throws IOException {
JSONObject jsonObject = new JSONObject();
if (ObjectUtil.isEmpty(batch)) {
throw new BusinessException("批次号不能为空");
}
if (ObjectUtil.isEmpty(file)) {
throw new BusinessException("文件不能为空");
}
String filename = file.getOriginalFilename();
String[] split = filename.split("\\.");
String name = split[0];
String suffix = split[1];
if (ObjectUtil.isEmpty(suffix)) {
throw new BusinessException("文件类型错误");
} else if (!"docx".equals(suffix)) {
throw new BusinessException("文件类型只支持docx类型");
}
byte[] bytes = file.getBytes();
InputStream inputStream = new ByteArrayInputStream(bytes);
String encode = Base64.encode(inputStream);
DateTime now = DateUtil.date();
String id = UUID.randomUUID().toString().replace("-", "");
String sql = "insert into api_file (id,batch,name,file,type,create_time) values (?,?,?,?,?,?)";
int update = jdbcTemplate.update(sql, id, batch, name, encode, suffix, now);
if (update > 0) {
jsonObject.put("code", 200);
jsonObject.put("msg", "上传成功");
} else {
jsonObject.put("code", 300);
jsonObject.put("msg", "上传失败");
}
return jsonObject;
}