Element-ui el-upload组件上传功能 前后台

1、前端代码


上传

2、后端代码

controller

// 处理文件上传
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request,UploadFileInfo vo) {

	String contentType = file.getContentType(); // 图片文件类型
	String fileName =	file.getOriginalFilename();	 // 图片名字
	UUID uuid=UUID.randomUUID();
	String newfileName = uuid.toString()+fileName.substring(fileName.indexOf('.')); //文件重命名
	//文件存放路径
	String filePath = "\\\\192.168.9.121\\fileServer\\";
	filePath = filePath+ vo.getYwzj() + "\\";
	try {
		FileUtil.uploadFile(file.getBytes(), filePath, newfileName);//文件处理
		vo.setFjmc(newfileName);
		vo.setFjlj(FileUtil.AttachmentServerPath +"/"+ vo.getYwzj() + "/" +newfileName);
		vo.setKzm(fileName.substring(fileName.indexOf('.')));
		vo.setFjwldz(filePath + newfileName);
		vo.setXsmc(fileName);
		vo.setFjdx(new Long(file.getSize()));
		fileUpdataService.save(vo);//文件路径存入表中
			
	} catch (Exception e) {
		// TODO: handle exception
	}
	// 返回图片的存放路径
	return FileUtil.AttachmentServerPath +"/"+ vo.getYwzj() + "/" +newfileName;
}	

FileUtil

public class FileUtil {
	
	public static final String AttachmentServerPath = "http://192.168.9.121:8088/fileServer";
	// 文件上传工具类服务方法
	public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
		File targetFile = new File(filePath);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		FileOutputStream out = new FileOutputStream(filePath + fileName);
		out.write(file);
		out.flush();
		out.close();
	}
}

 

你可能感兴趣的:(Java,vue)