public class PublicFile {
private String fileName;
private String fileUrl;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileUrl() {
return fileUrl;
}
public void setFileUrl(String fileUrl) {
this.fileUrl = fileUrl;
}
}
public static Object upload(MultipartFile file){
PublicFile publicFile=new PublicFile();
if(file.isEmpty()){
System.out.println("文件不能为空");
return "文件不能为空";
}else{
String Oldfilename=file.getOriginalFilename();
String filepath=new File("Web/upload").getAbsolutePath();
String uuid= UUID.randomUUID().toString().replaceAll("-","");
String suffix=Oldfilename.substring(Oldfilename.lastIndexOf("."));
String newfilename=uuid+suffix;
File dest=new File(filepath+"/"+newfilename);
publicFile.setFileName(Oldfilename);
publicFile.setFileUrl("Web/upload/"+newfilename);
try {
file.transferTo(dest);
LOGGER.info("上传成功");
return publicFile;
} catch (IOException e) {
LOGGER.error(e.toString(), e);
}
return "上传失败!";
}
}
public static Object uploads(MultipartFile[] file){
List<PublicFile> publicFileList=new ArrayList<>();
String filepath=new File("Web/upload").getAbsolutePath();
if(0>file.length){
System.out.println("文件不能为空");
return "文件不能为空";
}else {
for (MultipartFile files : file) {
PublicFile publicFile = new PublicFile();
String Oldfilename = files.getOriginalFilename();
String uuid= UUID.randomUUID().toString().replaceAll("-","");
String suffix=Oldfilename.substring(Oldfilename.lastIndexOf("."));
String newfilename=uuid+suffix;
File dest = new File(filepath + "/" + newfilename);
publicFile.setFileName(Oldfilename);
publicFile.setFileUrl("Web/upload/" + newfilename);
publicFileList.add(publicFile);
try {
files.transferTo(dest);
} catch (IOException e) {
LOGGER.error(e.toString(), e);
return "上传失败!";
}
}
LOGGER.info("上传成功");
return publicFileList;
}
}
@RequestMapping(value="/insertuser",method = RequestMethod.POST)
private Map<String,Object> insertUser(HttpServletRequest request,User user,MultipartFile fileone,MultipartFile filetwo) throws IOException {
Map<String,Object> modeMap=new HashMap<>();
PublicFile publicFile= (PublicFile) FileUpload.upload(file);
PublicFile publicFile2= (PublicFile) FileUpload.upload(file2);
user.setFileName(publicFile.getFileName());
user.setFileUrl(publicFile.getFileUrl());
modeMap.put("success",userService.insertUser(user));
return modeMap;
}
public static String downloadFile(HttpServletResponse response, String fileName) {
File path = null;
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
try {
response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
path=new File("").getAbsoluteFile();
os = response.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(new File(path + "/Web/upload/" + fileName)));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (FileNotFoundException e1) {
return "系统找不到指定的文件";
}catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "success";
}