Dubbo在2.6版本后合并了dubbox的resteasy代码后,可以支持rest风格的接口发布,但是在使用form表单上传文件的时候,获取的文件名称是乱码。
下面通过对源码分析一下原因,并提供一种可行的解决方法。
首先是一个resteasy上传的使用代码
@POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) @Override public Object uploadfile(MultipartFormDataInput input, @Context HttpServletRequest request, @Context HttpServletResponse response) { System.out.println("进入业务逻辑"); // MultipartFormDataReader Map<String, Object> map = null; Map<String, List<InputPart>> uploadForm = input.getFormDataMap(); //取得文件表单名 List<InputPart> inputParts = uploadForm.get("file"); final String DIRCTORY = "D:/temp/datainput/"; initDirectory(DIRCTORY); InputStream inputStream = null; OutputStream outStream = null; for (InputPart inputPart : inputParts) { try { // 文件名称 String fileName = getFileName(inputPart.getHeaders()); inputStream = inputPart.getBody(InputStream.class, null); //把文件流保存; File file = new File(DIRCTORY + fileName); byte[] buffer = new byte[inputStream.available()]; inputStream.read(buffer); outStream = new FileOutputStream(file); outStream.write(buffer); } catch (IOException e) { e.printStackTrace(); }finally { if(null != inputStream){ try { inputStream.close(); } catch (IOException e) { } } if(null != outStream){ try { outStream.close(); } catch (IOException e) { } } } } return Response.ok().build(); }