搭建的思路图如下:
具体代码如下:
A.java本地java项目(关键代码)
public class SysOssController { private String remoteServer = "http://x.x.x.x:xxxx"; private String remoteFileUri = "http://http://x.x.x.x:xxxx/uploadservice/uploadService"; /** * 上传文件 */ @RequestMapping("/upload") // @RequiresPermissions("sys:oss:all") public R upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception { if (file.isEmpty()) { throw new RRException("上传文件不能为空"); } String fileName = StringUtils.cleanPath(file.getOriginalFilename()); String prefix = UUID.randomUUID().toString().replaceAll("-", ""); String suffix = fileName.substring(fileName.lastIndexOf(".")); File tempFile = null; String url = ""; //上传到指定的服务器项目 if(ConstantUtil.UPLOADSWITCH==1){ try { tempFile = File.createTempFile(prefix, suffix); if (!file.isEmpty()) { String serverUrl = remoteFileUri + "/save"; RestTemplate restTemplate = new RestTemplate(); file.transferTo(tempFile); //构建 HttpEntity HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); body.add("file", new FileSystemResource(tempFile)); HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers); ResponseEntity re = restTemplate.postForEntity(serverUrl, requestEntity, String.class); if(re.getStatusCode().value()==200){ url = re.getBody()+""; }else { url= ""; } } }catch (Exception e){ e.printStackTrace(); }finally { if (tempFile != null) { tempFile.delete(); } } }else if (ConstantUtil.UPLOADSWITCH==2){ //上传到云文件服务器上 //上传文件 url = OSSFactory.build().upload(file); } if(!StringUtils.isEmpty(url)){ //保存文件信息 SysOssEntity ossEntity = new SysOssEntity(); ossEntity.setUrl(url); ossEntity.setCreateTime(new Date()); sysOssService.save(ossEntity); } R r = new R(); r.put("url", remoteServer + url); r.put("link", url); return r; }
B 远程服务器上的 文件上传服务项目(关键代码)
@Controller @CrossOrigin @RequestMapping("/uploadService") public class FileUploadController { @PostMapping("/save") @ResponseBody public String handleFileUploadSave(@RequestParam("file") MultipartFile file) { String filename = ""; // MultipartFile是对当前上传的文件的封装,当要同时上传多个文件时,可以给定多个MultipartFile参数(数组) if (!file.isEmpty()) { String type = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));// 取文件格式后缀名 filename = System.currentTimeMillis() + type;// 取当前时间戳作为文件名 // String path = "d:/"+filename;// 存放位置 //String path = request.getSession().getServletContext().getRealPath("/upload/" + filename);// 存放位置 // String path = "D:/load/tomcat8.0.53/webapps/upload/images/" + filename; // String path = "D:/upload/images/" + filename; String path = "/usr/local/upload/images/" + filename; File destFile = new File(path); try { // FileUtils.copyInputStreamToFile()这个方法里对IO进行了自动操作,不需要额外的再去关闭IO流 FileUtils.copyInputStreamToFile(file.getInputStream(), destFile);// 复制临时文件到指定目录下 } catch (IOException e) { e.printStackTrace(); } } return "/upload/images/" + filename; } }
c 远程服务器保存的目录
/usr/local/upload/images
d 远程服务器上的web服务器tomcat的虚拟目录的配置
conf/server.xml <Context docBase="/usr/local/upload/images" path="/upload/images" reloadable="true"/> </Host>
e数据库的保存文件目录url如下(存的是相对路径,显示的时候在后台a项目上拼上远程tomcat服务地址):
a项目返回给浏览器回显url时候,只显示文件名(如果想显示全路径可以不做处理)
<Form-item label="照片" prop="picUrl"> <i-hidden v-model="lwHomePicture.url" placeholder="请上传宽1200像素,高60像素的图片" readonly/> <i-input v-model="picUrl" placeholder="请上传宽1200像素,高60像素的图片" readonly/> </Form-item>
let vm = new Vue({ el: '#rrapp', data: { picUrl:"" }, methods:{ getInfo: function(id){ Ajax.request({ url: "../lwhomepicture/info/"+id, async: true, successCallback: function (r) { vm.lwHomePicture = r.lwHomePicture; var url = vm.lwHomePicture.url; if(url)vm.picUrl = url.substring((url.lastIndexOf("\/") + 1), url.length); } }); }, handleSuccess: function (res, file) { vm.lwHomePicture.url = file.response.url; var url = file.response.url; if(url)vm.picUrl = url.substring((url.lastIndexOf("\/") + 1), url.length); }, }