java ibatis 读取mysql blob类型乱码 spring mvc文件下载csv文件乱码

java 中实体类类型  string   mysql  中blob  读取后乱码


一、

将实体类中类型修改为  

byte[] contentDesc;

sql-map中修改

<result column="ContentDesc" jdbcType="BLOB" property="contentDesc" javaType="byte" />

输出时

System.out.println(new String(wxcircleDTO.getContentDesc(),"UTF-8"));


输出中文了



spring   mvc  下载文件

读取数据库数据,保存为文件后下载。csv文件中文乱码

outwriter = new OutputStreamWriter(fileout,"GBK");

一定要是GBK,UTF-8会乱码掉

生成文件以及下载action方法

@RequestMapping(value="/downloadCSV.do")
	public ResponseEntity<byte[]> downloadCSV(HttpServletResponse res){
		
		List<WxDataDto> list = wxcircleDao.getDatas();
		
		System.out.println("数据大小:  " + list.size());
		FileOutputStream fileout = null;
		OutputStreamWriter outwriter = null;
		BufferedWriter bufferwri = null;
		CSVWriter csvwr = null;
		String save = "C:\\Users\\data.csv";
		try {
			fileout = new FileOutputStream(save);
			outwriter = new OutputStreamWriter(fileout,"GBK");
			bufferwri = new BufferedWriter(outwriter);
			csvwr = new CSVWriter(bufferwri,',');
			
			if(list != null && list.size() != 0){
				
				//写入头
				String[] record = {"t_id(表id)","id","username","createTime","private1",
									"contentDesc","city","longitude","latitude","poiAddr",
									"contentUrl","poiName", "sourceNickName","sourceUserName",
									"publicUserName","statictData","title","description","contentStyle",
									"mediaid","mediaurl","mediathumb","mediaDescription"};
				
				csvwr.writeNext(record);//写入新行数据
				
				for (WxDataDto wxcircleDTO : list) {
//					if(wxcircleDTO.getT_id() != 155)
//						continue;
//					bufferwri.append(wxcircleDTO.getId() + ",");
					
					record[0] = wxcircleDTO.getT_id() + "";
					record[1] = wxcircleDTO.getId() + "";
					record[2] = wxcircleDTO.getUsername();
					SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
					record[3] = format.format(Long.parseLong(wxcircleDTO.getCreateTime()) * 1000);
					record[4] = wxcircleDTO.getPrivate1() + "";
					record[5] = new String(wxcircleDTO.getContentDesc(),"UTF-8");
					record[6] = wxcircleDTO.getCity();
					record[7] = wxcircleDTO.getLongitude();
					record[8] = wxcircleDTO.getLatitude();
					record[9] = wxcircleDTO.getPoiAddr();
					record[10] = wxcircleDTO.getContentUrl();
					record[11] = wxcircleDTO.getPoiName();
					record[12] = wxcircleDTO.getSourceNickName();
					record[13] = wxcircleDTO.getSourceUserName();
					record[14] = wxcircleDTO.getPublicUserName();
					record[15] = wxcircleDTO.getStatictData();
					record[16] = wxcircleDTO.getTitle();
					record[17] = wxcircleDTO.getDescription();
					record[18] = wxcircleDTO.getContentStyle();
					record[19] = wxcircleDTO.getMediaid() + "";
					record[20] = wxcircleDTO.getMediaurl();
					record[21] = wxcircleDTO.getMediathumb();
					record[22] = wxcircleDTO.getMediaDescription();
					
					csvwr.writeNext(record);//写入新行数据
					csvwr.flush();
//					String DEFAULT_CHARSET = "UTF-8";
//					ByteArrayInputStream bis = new ByteArrayInputStream(wxcircleDTO.getContentDesc().getBytes(DEFAULT_CHARSET));  
//					StringBuffer sb = new StringBuffer(1024);
//					int value = 0;
//					while((value = bis.read()) != -1){
//						sb.append((char)value);
//					}
//			        byte[] returnValue = null;  
//			        if (null != blob) {  
//			            returnValue = blob.getBytes(1, (int) blob.length());  
//			        }  
//					String desc = new String(.getBytes(),);
//					System.out.println(sb.toString());
//					System.out.println(new String(wxcircleDTO.getContentDesc().getBytes(),"UTF-8"));
					
//					bufferwri.append(wxcircleDTO.getT_id() + "");
//					System.out.println(wxcircleDTO.getContentDesc().toString());
//					bufferwri.append(wxcircleDTO.getContentDesc() + ",");
//					bufferwri.append(wxcircleDTO.getContentStyle() + ",");
//					bufferwri.append(wxcircleDTO.getContentUrl() + ",");
//					SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
//					String time = format.format(Long.parseLong(wxcircleDTO.getCreateTime()) * 1000);
//					bufferwri.append(time + ",");
//					bufferwri.append(wxcircleDTO.getDescription() + ",");
//					bufferwri.append(wxcircleDTO.getLatitude());
					
//					bufferwri.append("\r\n");
					csvwr.flush();
				}
				
			}
			bufferwri.flush();
			
			
			/**
			 * 不可以打印否则无法下载
			 */
//			FileInputStream fi = new FileInputStream(save);
//			OutputStream out = res.getOutputStream();
//			int dex;
//			while((dex = fi.read()) != -1){
//				out.write(dex);
//			}
//			out.flush();
//			
//			if(out != null){
//				out.close();
//			}
//			if(fi != null){
//				fi.close();
//			}
			
//			File fi = new File(save);
//			InputStream is = new FileInputStream(fi);
//			int dex;
//			while((dex = is.read()) != -1){
//				res.getOutputStream().write(dex);
//			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(csvwr != null){
				try {
					csvwr.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(bufferwri != null){
				try {
					bufferwri.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(outwriter != null){
				try {
					outwriter.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(fileout != null){
				try {
					fileout.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	    /**
	     * 返回页面下载
	     */
	    try {
	    	HttpHeaders headers = new HttpHeaders();  
	    	headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);  
	    	headers.setContentDispositionFormData("attachment", "data_.csv");  
			return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File(save)),  
			                                  headers, HttpStatus.CREATED);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return null;
	}

spring-servlet。xml配置

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">      
        <property name="messageConverters">      
            <list >      
            	<!-- //把ByteArray加在Json前面 必须加入,否则无法下载   文件下载byte方式   -->
            	<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>  
                <ref bean="mappingJacksonHttpMessageConverter" />      
            </list>      
        </property>      
    </bean>
mysql  blob 对应java

byte[] contentDesc;//

sql -map
<pre name="code" class="html"><result column="ContentDesc" jdbcType="BLOB" property="contentDesc" javaType="byte" />


 
 


你可能感兴趣的:(ibatis)