文件上传 bast64
This commit is contained in:
parent
0b419f8c5f
commit
c7b8378b78
|
|
@ -99,8 +99,8 @@ public class PartApplyAppController {
|
|||
* @return
|
||||
*/
|
||||
@PostMapping("uploadImage")
|
||||
public ServerResponse uploadImage(HttpServletRequest request, @RequestParam("file[]") MultipartFile[] files) {
|
||||
return service.uploadImage(request,files);
|
||||
public ServerResponse uploadImage(HttpServletRequest request) {
|
||||
return service.uploadImage(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -146,20 +146,22 @@ public class PartApplyAppServiceImp {
|
|||
/**
|
||||
* 上传图片
|
||||
* @param request
|
||||
* @param files
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
public ServerResponse uploadImage(HttpServletRequest request, MultipartFile[] files) {
|
||||
public ServerResponse uploadImage(HttpServletRequest request) {
|
||||
try{
|
||||
String id=request.getParameter("params");
|
||||
String bast64s=request.getParameter("bast64");
|
||||
List<String> files=JSON.parseArray(bast64s,String.class);
|
||||
if(StringHelper.isEmpty(id)){
|
||||
return ServerResponse.createErroe("请选择申请记录");
|
||||
}
|
||||
if(files==null || files.length<=0){
|
||||
if(files==null || files.size()<1){
|
||||
return ServerResponse.createErroe("请先上传文件");
|
||||
}
|
||||
List<FileUploadVo> fileList=uploadService.uploadImage(files,id,"t_part_apply","使用照片");
|
||||
if(StringUtils.isEmpty(fileList) || fileList.size()!=files.length){
|
||||
List<FileUploadVo> fileList=uploadService.uploadImageBast64(files,id,"t_part_apply","使用照片");
|
||||
if(StringUtils.isEmpty(fileList) || fileList.size()!=files.size()){
|
||||
return ServerResponse.createErroe("文件上传失败");
|
||||
}
|
||||
return ServerResponse.createSuccess("文件上传成功","文件上传成功");
|
||||
|
|
@ -167,7 +169,6 @@ public class PartApplyAppServiceImp {
|
|||
log.error(e.toString(),e);
|
||||
}
|
||||
return ServerResponse.createErroe("文件上传失败");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -10,10 +10,10 @@ import com.github.pagehelper.PageHelper;
|
|||
import com.github.pagehelper.PageInfo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
|
@ -73,8 +73,14 @@ public class PartApplyController {
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 图片文件上传
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("uploadImage")
|
||||
public ServerResponse uploadImage(HttpServletRequest request, @RequestParam("file[]") MultipartFile[] files) {
|
||||
return service.uploadImage(request,files);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@ package com.bonus.gzgqj.business.bases.service;
|
|||
|
||||
import com.bonus.gzgqj.business.app.entity.PartApplyAppVo;
|
||||
import com.bonus.gzgqj.manager.webResult.ServerResponse;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
|
@ -30,4 +32,12 @@ public interface PartApplyService {
|
|||
* @return
|
||||
*/
|
||||
ServerResponse auditData(PartApplyAppVo data);
|
||||
|
||||
/**
|
||||
* 图片文件上传
|
||||
* @param request
|
||||
* @param files
|
||||
* @return
|
||||
*/
|
||||
ServerResponse uploadImage(HttpServletRequest request, MultipartFile[] files);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,12 +7,15 @@ import com.bonus.gzgqj.business.bases.mapper.PartApplyMapper;
|
|||
import com.bonus.gzgqj.business.plan.entity.FileUploadVo;
|
||||
import com.bonus.gzgqj.business.utils.FileUploadService;
|
||||
import com.bonus.gzgqj.manager.common.util.StringHelper;
|
||||
import com.bonus.gzgqj.manager.common.util.StringUtils;
|
||||
import com.bonus.gzgqj.manager.common.util.UserUtil;
|
||||
import com.bonus.gzgqj.manager.webResult.ServerResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
|
@ -121,6 +124,33 @@ public class PartApplyServiceImpl implements PartApplyService{
|
|||
return ServerResponse.createErroe("审核失败");
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
* @param request
|
||||
* @param files
|
||||
* @return
|
||||
*/
|
||||
public ServerResponse uploadImage(HttpServletRequest request, MultipartFile[] files) {
|
||||
try{
|
||||
String id=request.getParameter("params");
|
||||
if(StringHelper.isEmpty(id)){
|
||||
return ServerResponse.createErroe("请选择出库记录");
|
||||
}
|
||||
if(files==null || files.length<=0){
|
||||
return ServerResponse.createErroe("请先上传文件");
|
||||
}
|
||||
List<FileUploadVo> fileList=uploadService.uploadImage(files,id,"t_part_apply","出库附件");
|
||||
if(StringUtils.isEmpty(fileList) || fileList.size()!=files.length){
|
||||
return ServerResponse.createErroe("文件上传成功");
|
||||
}
|
||||
return ServerResponse.createSuccess("文件上传成功","文件上传成功");
|
||||
}catch (Exception e){
|
||||
log.error(e.toString(),e);
|
||||
}
|
||||
return ServerResponse.createErroe("文件上传失败");
|
||||
|
||||
}
|
||||
|
||||
|
||||
public String transBast64(String url){
|
||||
try{
|
||||
|
|
|
|||
|
|
@ -15,8 +15,11 @@ import javax.swing.*;
|
|||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
import static com.bonus.gzgqj.business.utils.IDUtils.getSuffix;
|
||||
import static com.bonus.gzgqj.business.utils.IDUtils.isImageFileExtension;
|
||||
|
||||
|
|
@ -33,6 +36,47 @@ public class FileUploadService {
|
|||
|
||||
|
||||
|
||||
|
||||
public List<FileUploadVo> uploadImageBast64(List<String> files , String outId, String table,String type){
|
||||
List<FileUploadVo> list=new ArrayList<>();
|
||||
try {
|
||||
for (String bast64 : files) {
|
||||
String fileName=IDUtils.createID()+".png";
|
||||
String filePath= DateTimeHelper.getNowYMD()+"/"+ fileName;
|
||||
String newPath= SystemUtils.getUploadPath()+filePath;
|
||||
// 解码Base64字符串
|
||||
byte[] decodedBytes = Base64.getDecoder().decode(bast64);
|
||||
try (FileOutputStream fileOutputStream = new FileOutputStream(filePath)) {
|
||||
// 将字节写入文件
|
||||
fileOutputStream.write(decodedBytes);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
FileUploadVo vo=new FileUploadVo();
|
||||
vo.setFileName(fileName);
|
||||
vo.setFileUrl(newPath);
|
||||
vo.setOwnId(outId);
|
||||
vo.setModelTable(table);
|
||||
vo.setSuffix(".png");
|
||||
vo.setFileType("1");
|
||||
String userId= UserUtil.getLoginUser().getUserId()+"";
|
||||
String userName=UserUtil.getLoginUser().getUsername();
|
||||
vo.setCreator(userId);
|
||||
vo.setCreateName(userName);
|
||||
if(StringHelper.isNotEmpty(type)){
|
||||
vo.setType(type);
|
||||
}
|
||||
list.add(vo);
|
||||
mapper.insertFileUpload(vo);
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.error(e.toString(),e);
|
||||
}
|
||||
return list;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*/
|
||||
|
|
@ -70,7 +114,6 @@ public class FileUploadService {
|
|||
mapper.insertFileUpload(vo);
|
||||
}
|
||||
|
||||
|
||||
}catch (Exception e){
|
||||
log.error(e.toString(),e);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue