乱码问题

大大阿 2014-02-20 10:06:11
public static String get(String Url){
fileUrl="F:/demo.doc";
File file = new File(Url);
StringBuilder sb = new StringBuilder();
String s ="";
try {
// BufferedReader br = new BufferedReader(new FileReader(file));
InputStreamReader in = new InputStreamReader(new FileInputStream(file), "gb2312");
BufferedReader br = new BufferedReader(in);
while( (s = br.readLine()) != null) {
sb.append(s + "\n");
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String str = sb.toString();
System.out.println(str);
return str;
}


是乱码,怎么解决?

我把 InputStreamReader in = new InputStreamReader(new FileInputStream(file), "gb2312");
改成utf-8 iso-8859-1都不行
...全文
177 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
小律律 2014-02-20
  • 打赏
  • 举报
回复
引用 18 楼 u013620582 的回复:
[quote=引用 17 楼 huanglin02 的回复:] [quote=引用 15 楼 ameyume 的回复:] POI logoOffice文档的Java处理包 POI Apache POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目。目前POI已经有了Ruby版本。 结构: HSSF - 提供读写Microsoft Excel XLS格式档案的功能。 XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。 HWPF - 提供读写Microsoft Word DOC格式档案的功能。 HSLF - 提供读写Microsoft PowerPoint格式档案的功能。 HDGF - 提供读Microsoft Visio格式档案的功能。 HPBF - 提供读Microsoft Publisher格式档案的功能。 HSMF - 提供读Microsoft Outlook格式档案的功能。 一段处理 EXCEL 文档的示例代码:
// Define a few rows
for(short rownum = (short)0; rownum < 30; rownum++) {
	HSSFRow r = s.createRow(rownum);
	for(short cellnum = (short)0; cellnum < 10; cellnum += 2) {
		HSSFCell c = r.createCell(cellnum);
		HSSFCell c2 = r.createCell(cellnum+1);

		c.setCellValue((double)rownum + (cellnum/10));
		c2.setCellValue(new HSSFRichTextString("Hello! " + cellnum);
	}
}
在线Javadoc:http://www.ostools.net/apidocs/apidoc?api=apache-POI
虽然我的 他不喜欢,也不跟我说一下 3Q~~~~(>_<)~~~~ 我委屈。。。。[/quote]喜欢 喜欢 谢谢~[/quote]这下心里平衡多了
大大阿 2014-02-20
  • 打赏
  • 举报
回复
引用 17 楼 huanglin02 的回复:
[quote=引用 15 楼 ameyume 的回复:] POI logoOffice文档的Java处理包 POI Apache POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目。目前POI已经有了Ruby版本。 结构: HSSF - 提供读写Microsoft Excel XLS格式档案的功能。 XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。 HWPF - 提供读写Microsoft Word DOC格式档案的功能。 HSLF - 提供读写Microsoft PowerPoint格式档案的功能。 HDGF - 提供读Microsoft Visio格式档案的功能。 HPBF - 提供读Microsoft Publisher格式档案的功能。 HSMF - 提供读Microsoft Outlook格式档案的功能。 一段处理 EXCEL 文档的示例代码:
// Define a few rows
for(short rownum = (short)0; rownum < 30; rownum++) {
	HSSFRow r = s.createRow(rownum);
	for(short cellnum = (short)0; cellnum < 10; cellnum += 2) {
		HSSFCell c = r.createCell(cellnum);
		HSSFCell c2 = r.createCell(cellnum+1);

		c.setCellValue((double)rownum + (cellnum/10));
		c2.setCellValue(new HSSFRichTextString("Hello! " + cellnum);
	}
}
在线Javadoc:http://www.ostools.net/apidocs/apidoc?api=apache-POI
虽然我的 他不喜欢,也不跟我说一下 3Q~~~~(>_<)~~~~ 我委屈。。。。[/quote]喜欢 喜欢 谢谢~
小律律 2014-02-20
  • 打赏
  • 举报
回复
引用 15 楼 ameyume 的回复:
POI logoOffice文档的Java处理包 POI Apache POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目。目前POI已经有了Ruby版本。 结构: HSSF - 提供读写Microsoft Excel XLS格式档案的功能。 XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。 HWPF - 提供读写Microsoft Word DOC格式档案的功能。 HSLF - 提供读写Microsoft PowerPoint格式档案的功能。 HDGF - 提供读Microsoft Visio格式档案的功能。 HPBF - 提供读Microsoft Publisher格式档案的功能。 HSMF - 提供读Microsoft Outlook格式档案的功能。 一段处理 EXCEL 文档的示例代码:
// Define a few rows
for(short rownum = (short)0; rownum < 30; rownum++) {
	HSSFRow r = s.createRow(rownum);
	for(short cellnum = (short)0; cellnum < 10; cellnum += 2) {
		HSSFCell c = r.createCell(cellnum);
		HSSFCell c2 = r.createCell(cellnum+1);

		c.setCellValue((double)rownum + (cellnum/10));
		c2.setCellValue(new HSSFRichTextString("Hello! " + cellnum);
	}
}
在线Javadoc:http://www.ostools.net/apidocs/apidoc?api=apache-POI
虽然我的 他不喜欢,也不跟我说一下 3Q~~~~(>_<)~~~~ 我委屈。。。。
大大阿 2014-02-20
  • 打赏
  • 举报
回复
引用 15 楼 ameyume 的回复:
POI logoOffice文档的Java处理包 POI Apache POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目。目前POI已经有了Ruby版本。 结构: HSSF - 提供读写Microsoft Excel XLS格式档案的功能。 XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。 HWPF - 提供读写Microsoft Word DOC格式档案的功能。 HSLF - 提供读写Microsoft PowerPoint格式档案的功能。 HDGF - 提供读Microsoft Visio格式档案的功能。 HPBF - 提供读Microsoft Publisher格式档案的功能。 HSMF - 提供读Microsoft Outlook格式档案的功能。 一段处理 EXCEL 文档的示例代码:
// Define a few rows
for(short rownum = (short)0; rownum < 30; rownum++) {
	HSSFRow r = s.createRow(rownum);
	for(short cellnum = (short)0; cellnum < 10; cellnum += 2) {
		HSSFCell c = r.createCell(cellnum);
		HSSFCell c2 = r.createCell(cellnum+1);

		c.setCellValue((double)rownum + (cellnum/10));
		c2.setCellValue(new HSSFRichTextString("Hello! " + cellnum);
	}
}
在线Javadoc:http://www.ostools.net/apidocs/apidoc?api=apache-POI
3Q~
ameyume 2014-02-20
  • 打赏
  • 举报
回复
POI logoOffice文档的Java处理包 POI Apache POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目。目前POI已经有了Ruby版本。 结构: HSSF - 提供读写Microsoft Excel XLS格式档案的功能。 XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。 HWPF - 提供读写Microsoft Word DOC格式档案的功能。 HSLF - 提供读写Microsoft PowerPoint格式档案的功能。 HDGF - 提供读Microsoft Visio格式档案的功能。 HPBF - 提供读Microsoft Publisher格式档案的功能。 HSMF - 提供读Microsoft Outlook格式档案的功能。 一段处理 EXCEL 文档的示例代码:
// Define a few rows
for(short rownum = (short)0; rownum < 30; rownum++) {
	HSSFRow r = s.createRow(rownum);
	for(short cellnum = (short)0; cellnum < 10; cellnum += 2) {
		HSSFCell c = r.createCell(cellnum);
		HSSFCell c2 = r.createCell(cellnum+1);

		c.setCellValue((double)rownum + (cellnum/10));
		c2.setCellValue(new HSSFRichTextString("Hello! " + cellnum);
	}
}
在线Javadoc:http://www.ostools.net/apidocs/apidoc?api=apache-POI
小律律 2014-02-20
  • 打赏
  • 举报
回复
引用 12 楼 ameyume 的回复:
[quote=引用 9 楼 huanglin02 的回复:] 1,用Stream读二进制文件 2,jdk不提供doc文档格式的解析功能 可以这样读取 文档内容 package com.fileupload.entity; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.hwpf.usermodel.CharacterRun; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.hwpf.usermodel.Section; public class TestDoc { // 直接抽取全部内容 public static String readDoc1(InputStream is) throws IOException { WordExtractor extractor = new WordExtractor(is); return extractor.getText(); } //分章节Section、段落Paragraph、字符串CharacterRun抽取 public static void readDoc2(InputStream is) throws IOException { HWPFDocument doc=new HWPFDocument(is); Range r=doc.getRange(); for(int x=0;x<r.numSections();x++){ Section s=r.getSection(x); for(int y=0;y<s.numParagraphs();y++){ Paragraph p=s.getParagraph(y); for(int z=0;z<p.numCharacterRuns();z++){ CharacterRun run=p.getCharacterRun(z); String text=run.text(); System.out.print(text); } } } } public static void main(String[] args) { File file = new File("d://1.doc"); try { FileInputStream fin = new FileInputStream(file); String cont = readDoc1(fin); System.out.println(cont); fin.close(); fin = new FileInputStream(file); readDoc2(fin); fin.close(); } catch (IOException e) { e.printStackTrace(); } } } 需要引入 poi-3.7.jat和poi-scratchpad-3.7.ajr这两个包。百度搜下载一下即可。我资源里面也有
up [/quote]
长笛党希望 2014-02-20
  • 打赏
  • 举报
回复
word、excel可以通过poi来解决,楼主可以上网搜搜poi的相关资料和教程。
ameyume 2014-02-20
  • 打赏
  • 举报
回复
引用 9 楼 huanglin02 的回复:
1,用Stream读二进制文件 2,jdk不提供doc文档格式的解析功能 可以这样读取 文档内容 package com.fileupload.entity; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.hwpf.usermodel.CharacterRun; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.hwpf.usermodel.Section; public class TestDoc { // 直接抽取全部内容 public static String readDoc1(InputStream is) throws IOException { WordExtractor extractor = new WordExtractor(is); return extractor.getText(); } //分章节Section、段落Paragraph、字符串CharacterRun抽取 public static void readDoc2(InputStream is) throws IOException { HWPFDocument doc=new HWPFDocument(is); Range r=doc.getRange(); for(int x=0;x<r.numSections();x++){ Section s=r.getSection(x); for(int y=0;y<s.numParagraphs();y++){ Paragraph p=s.getParagraph(y); for(int z=0;z<p.numCharacterRuns();z++){ CharacterRun run=p.getCharacterRun(z); String text=run.text(); System.out.print(text); } } } } public static void main(String[] args) { File file = new File("d://1.doc"); try { FileInputStream fin = new FileInputStream(file); String cont = readDoc1(fin); System.out.println(cont); fin.close(); fin = new FileInputStream(file); readDoc2(fin); fin.close(); } catch (IOException e) { e.printStackTrace(); } } } 需要引入 poi-3.7.jat和poi-scratchpad-3.7.ajr这两个包。百度搜下载一下即可。我资源里面也有
up
loveunittesting 2014-02-20
  • 打赏
  • 举报
回复
office基本上可以用poi,但是写法不一样。其他的基本上得想其他办法
大大阿 2014-02-20
  • 打赏
  • 举报
回复
引用 8 楼 yys79 的回复:
word可以用poi读,pdf没读过,不知道pdfbox是否可以。txt用你这个代码倒是可以
还有excel,ppt多种格式。。要怎么写才能最优化?还是我需要一个一个去写?
小律律 2014-02-20
  • 打赏
  • 举报
回复
1,用Stream读二进制文件 2,jdk不提供doc文档格式的解析功能 可以这样读取 文档内容 package com.fileupload.entity; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.hwpf.usermodel.CharacterRun; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.hwpf.usermodel.Section; public class TestDoc { // 直接抽取全部内容 public static String readDoc1(InputStream is) throws IOException { WordExtractor extractor = new WordExtractor(is); return extractor.getText(); } //分章节Section、段落Paragraph、字符串CharacterRun抽取 public static void readDoc2(InputStream is) throws IOException { HWPFDocument doc=new HWPFDocument(is); Range r=doc.getRange(); for(int x=0;x<r.numSections();x++){ Section s=r.getSection(x); for(int y=0;y<s.numParagraphs();y++){ Paragraph p=s.getParagraph(y); for(int z=0;z<p.numCharacterRuns();z++){ CharacterRun run=p.getCharacterRun(z); String text=run.text(); System.out.print(text); } } } } public static void main(String[] args) { File file = new File("d://1.doc"); try { FileInputStream fin = new FileInputStream(file); String cont = readDoc1(fin); System.out.println(cont); fin.close(); fin = new FileInputStream(file); readDoc2(fin); fin.close(); } catch (IOException e) { e.printStackTrace(); } } } 需要引入 poi-3.7.jat和poi-scratchpad-3.7.ajr这两个包。百度搜下载一下即可。我资源里面也有
loveunittesting 2014-02-20
  • 打赏
  • 举报
回复
word可以用poi读,pdf没读过,不知道pdfbox是否可以。txt用你这个代码倒是可以
大大阿 2014-02-20
  • 打赏
  • 举报
回复
引用 4 楼 yys79 的回复:
doc本来就是二进制文件,根本也不是能读出来的。比如你用这种方式读个jpg图片文件,能读出来文本吗?
我现在想把word,text,pdf文件读出来写进xml。
loveunittesting 2014-02-20
  • 打赏
  • 举报
回复
doc本来就是二进制文件,根本也不是能读出来的。比如你用这种方式读个jpg图片文件,能读出来文本吗?
笨笨的_ 2014-02-20
  • 打赏
  • 举报
回复
处理方法一: a标签,超连接传值到后台乱码: byte[] data = request.getParameter("title") .getBytes("ISO-8859-1") ; String title = new String(data, "UTF-8"); **********************************************************华丽的分割线************************************************** 处理方法二: tomcat乱码: 在类中添加如下三句代码进行处理, request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); 并在tomcat修改端口的地方 加上这样一句代码:URIEncoding="UTF-8"; **********************************************************华丽的分割线************************************************** 处理方法三:java自带的转换工具; //java转成%到服务器 String desc=java.net.URLEncoder.encode(java.net.URLEncoder.encode(content.toString(),"UTF-8"),"UTF-8"); 在服务器用: String desc=request.getAttribute("desc").toString(); desc=java.net.URLDecoder.decode(java.net.URLDecoder.decode(desc,"UTF-8"),"UTF-8"); 转换成中文; **********************************************************华丽的分割线************************************************** //js转%到服务器 var desc=escape(escape(desc)); 在服务器用: String desc=StringUtil.unescape(desc);
大大阿 2014-02-20
  • 打赏
  • 举报
回复
引用 1 楼 yys79 的回复:
你读的文件是doc格式?那也不是文本啊,按文本呢读能读出来东西才怪呢
你知道如何解决吗?
loveunittesting 2014-02-20
  • 打赏
  • 举报
回复
你读的文件是doc格式?那也不是文本啊,按文本呢读能读出来东西才怪呢

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧