I am developing both client and service side of a Rest Web Service.
Client side is a web application that is using Jersey client api to call the service that is written using Spring's support of Rest.
Client is trying to send a file that is written using Window's notepad and saved by selecting ANSI from encoding list box that appears at the time of saving a notepad.
on Service side,I have a command class that has a field of byte[].
This is the code on server side -
My web.xml on service side has the filter set as follows -Code:@RequestMapping(method = RequestMethod.POST, value = "/request") public ModelAndView requestTranslation( @ModelAttribute("clientCommand") ClientCommand clientCommand, HttpServletRequest request, HttpServletResponse response) throws Exception { FileOutputStream fos = new FileOutputStream("input.txt") ; fos.write(clientCommand.getUploadFile()) ; fos.flush(); fos.close() ; return null ; } @InitBinder protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws ServletException { // to actually be able to convert Multipart instance to byte[] we have // to register a custom editor // now Spring knows how to handle multipart object and convert them binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); }
When I check input.txt,one of the characters that belongs to extended ASCII character sets ’ (ASCII value 146) becomes garbled (? or blank square) .Code:<filter> <filter-name>charsetFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>charsetFilter</filter-name> <url-pattern>/service/request</url-pattern> </filter-mapping>
In case it is needed,my client side code is as follows -
Same file when saved in Notepad using UTF-8 encoding,works absolutely fine without any complaints at all.Code:protected String forwardToTranslation(@ModelAttribute("testCommand") TestCommand testCommand,HttpServletRequest request,HttpServletResponse httpResponse) throws Exception { byte[] uploadFile = testCommand.getUploadFile() ; Client client = Client.create(); WebResource service = client.resource(AltCsoUtil.loadProperties().getProperty("requestUrl")); FormDataMultiPart formData = new FormDataMultiPart() ; formData.field("sourceLanguage",sourceLanguage) ; formData.field("targetLanguage",targetLanguage) ; formData.field("inputFormat",inputFormat) ; formData.field("tmx",tmx) ; formData.field("uploadFile", uploadFile, MediaType.MULTIPART_FORM_DATA_TYPE); formData.field("priority",priority) ; formData.field("profile", profile) ; formData.field("uploadFileName", uploadFileName) ; ClientResponse response = service.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class,formData); return "myapp" ; }
Can someone please help?


Reply With Quote