springmvc implementation file download

//在springmvc中配置
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
               <bean class="org.springframework.http.converter.StringHttpMessageConverter" />


//controller写法
@RequestMapping("download.do")
    public ResponseEntity<byte[]> download(HttpServletRequest request,HttpServletResponse response,String pictureFile) throws Exception{
        File file=new File("F:\\upload\\image\\"+pictureFile);  
        HttpHeaders headers = new HttpHeaders();    
        pictureFile = new String(pictureFile.getBytes("UTF-8"),"iso-8859-1"); // In order to solve the problem of garbled Chinese names   
        headers.setContentDispositionFormData("attachment" , pictureFile);   
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);   
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
                                          headers, HttpStatus.CREATED);    
        
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325341163&siteId=291194637