dubbo的Multipart文件上传问题

采用dubbo对MultipartFile文件进行传输时,会报invoke method 错误,是因为dubbo并不能跨系统传递这个对象,可以采用Hessian

		 
            com.caucho
            hessian
            4.0.7
        
        
            org.mortbay.jetty
            jetty
            6.1.26
            
	     		
		     		org.mortbay.jetty
					servlet-api
				
			
        

要注意,jetty包中要排除servlet-api包的依赖,否则会报错,Tomcat不能正常启动。
然后在spring中暴露接口:

 
    
    
    
    
    

    
    
    

另外,dubbo跨系统传递流,会使得流为空,最简单有效的方法是将流转变为byte数组,然后再将byte数组转化为流,即用byte为中介传递

	@RequestMapping("/pic/upload")
	@ResponseBody
	public PictureResult upload(@RequestParam MultipartFile uploadFile) throws IOException {
		System.out.println(uploadFile.getSize());
		byte[] pic = uploadFile.getBytes();
        PictureResult result = pictureService.uploadPicture(pic, uploadFile);
	    return result;
	}
@Override
	public PictureResult uploadPicture(byte[] pic, MultipartFile uploadFile) {
		if(pic == null || pic.length == 0) {
			return PictureResult.error("空文件");
		}
		String oldName = uploadFile.getOriginalFilename();
		String newName = UUID.randomUUID() + oldName.substring(oldName.lastIndexOf("."));
		
		String filePath = new DateTime().toString("/yyyy/MM/dd");
		boolean res;
		res = FtpUtils.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USER_NAME, FTP_PASSWORD,
				FTP_BASE_PATH, filePath, newName, new ByteArrayInputStream(pic));
		if(!res) {
			return PictureResult.error("上传失败");
		}
		String url = FTP_BASE_PATH + filePath + "/" + newName;
		return PictureResult.ok(url);
	}

你可能感兴趣的:(Java)