1,可能是BUFFER的原因!
2,两者的差别很大!DataInputStream有很多独有的方法能读取文件中的数据类型.
3,是的!
2,两者的差别很大!DataInputStream有很多独有的方法能读取文件中的数据类型.
3,是的!
解决方案 »
- JAVA有关变量的问题,我不知道怎么打开我编辑好的CLASS文件
- IO流找错,在线等20分
- 精度问题讨论
- SQL server2000 与java的连接问题
- 新手求教
- 关于 网络连接 线程问题的请教!
- 初学者的小问题!
- java.sql.ResultSet这个类怎么使用???
- 用Javamail发送带图片的HTML File(当作邮件的Body)成功,用Outlook收可以正常显示?但是用Lotus Notes收却看不到?
- jdk怎么不好用?
- 安装了了JB6,还能再安装JDK1。4吗??
- 在html运行的applet中需要访问oracle,在jb里可以连接,但直接点击html时就出现找不到类“sun.jdbc.odbd.JdbcOdbcDriver"?
是不是BufferedInputStream读不了文件中的数据?比如象word,excel,gif等文件中的数据?
Class BufferedInputStream
java.lang.Object
|
+--java.io.InputStream
|
+--java.io.FilterInputStream
|
+--java.io.BufferedInputStream
--------------------------------------------------------------------------------public class BufferedInputStream
extends FilterInputStream
A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the and reset methods. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time. The operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent operation to be reread before new bytes are taken from the contained input stream.
Class DataInputStream
java.lang.Object
|
+--java.io.InputStream
|
+--java.io.FilterInputStream
|
+--java.io.DataInputStreamAll Implemented Interfaces:
DataInput --------------------------------------------------------------------------------public class DataInputStream
extends FilterInputStream
implements DataInput
A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream. Data input streams and data output streams represent Unicode strings in a format that is a slight modification of UTF-8. (For more information, see X/Open Company Ltd., "File System Safe UCS Transformation Format (FSS_UTF)", X/Open Preliminary Specification, Document Number: P316. This information also appears in ISO/IEC 10646, Annex P.) Note that in the following tables, the most significant bit appears in the far left-hand column. All characters in the range '\u0001' to '\u007F' are represented by a single byte: 0 bits 6-0 The null character '\u0000' and characters in the range '\u0080' to '\u07FF' are represented by a pair of bytes: 1 1 0 bits 10-6
1 0 bits 5-0
Characters in the range '\u0800' to '\uFFFF' are represented by three bytes:
1 1 1 0 bits 15-12
1 0 bits 11-6
1 0 bits 5-0 The two differences between this format and the "standard" UTF-8 format are the following: The null byte '\u0000' is encoded in 2-byte format rather than 1-byte, so that the encoded strings never have embedded nulls.
Only the 1-byte, 2-byte, and 3-byte formats are used.
程序片断如下:
public void uploadFile(javax.servlet.http.HttpServletRequest req) throws IOException,Exception {
try {
String contentType=req.getContentType();
int contentLength=req.getContentLength();
java.io.DataInputStream dis=new java.io.DataInputStream(req.getInputStream());
//此句改成如下就只能读文本文件正常,其他文件读取就不正常。不知为何! java.io.BufferedInputStream bis=new java.io.BufferedInputStream(req.getInputStream());
int once = 0;
int total = 0;
byte[] buffer=new byte[contentLength];
while ((total<contentLength) && (once>=0)) {
once = dis.read(buffer,total,contentLength);
total += once;
}
int boundaryStart=contentType.indexOf("boundary=");
boundaryStart=boundaryStart+"boundary=".length();
String boundary="--"+contentType.substring(boundaryStart); int pos=getFormNameIndex(buffer,"filename=\"".getBytes(),1)+"filename=\"".length();
int posEnd=getFormNameIndex(buffer,"\"".getBytes(),pos);
String filename=new String(buffer,pos,posEnd-pos);
pos=filename.lastIndexOf(".");
String filenames=System.currentTimeMillis()/1000+filename.substring(pos);
pos=getFormNameIndex(buffer,"Content-Type: ".getBytes(),1);
pos=getFormNameIndex(buffer,"\r\n".getBytes(),pos+1);
int endpos=getFormNameIndex(buffer,boundary.getBytes(),pos+20);
int len=endpos-pos;
System.out.println("get content:"+new String(buffer,pos,len)); //java.io.DataOutputStream bos= new java.io.DataOutputStream(new java.io.FileOutputStream(new java.io.File(filepath,filenames)));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(filename));
bos.write(buffer,pos,len); dis.close();
bos.close();
}catch(Exception exc) {
throw new Exception("upload failed.");
}finally{ }
不好意思,看不懂是何意思,能否给解释一下?谢谢!
1 1 0 bits 10-6
1 0 bits 5-0
等不知是何意思
BufferedInputStream是FileterInputStream子类,避免每次都物理得读取,告诉从缓冲区读,和FileInputSteam等InputStream子类联合使用(非FileterInputStream).