Results 1 to 2 of 2

Thread: Problem while converting ANSI to UTF-8

  1. #1

    Default Problem while converting ANSI to UTF-8

    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 -
    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());
    	}
    My web.xml on service side has the filter set as follows -
    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>
    When I check input.txt,one of the characters that belongs to extended ASCII character sets ’ (ASCII value 146) becomes garbled (? or blank square) .

    In case it is needed,my client side code is as follows -

    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" ;
    }
    Same file when saved in Notepad using UTF-8 encoding,works absolutely fine without any complaints at all.
    Can someone please help?
    Last edited by jiteshks; Jul 23rd, 2011 at 12:36 AM.

  2. #2

    Default

    Can someone please respond to this?Since there is very less manipulation going on,I suspect there is some fundamental mistake somewhere.
    I can post additional info if needed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •