当前位置: 技术问答>java相关
只已知一个InputStream is = getStream();请问如何知道is究竟有多少size ?
来源: 互联网 发布时间:2015-09-03
本文导语: 只已知一个InputStream is = getStream();请问如何知道is究竟有多少size ? | 如果是javamail中的得到附件的大小是用part.getSize(). 否则好象没有办法直接得到流的大小。 | 哦,这样...
只已知一个InputStream is = getStream();请问如何知道is究竟有多少size ?
|
如果是javamail中的得到附件的大小是用part.getSize().
否则好象没有办法直接得到流的大小。
否则好象没有办法直接得到流的大小。
|
哦,这样啊。
看看下面的上传文件的代码,你就知道了:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class UpLoadServlet extends HttpServlet {
private final String CONTENT_TYPE = "text/html;charset=gb2312";
private final int MACLEN = 2048 * 1024;
private final int BUFFERSIZE = 1024 * 8;
private final String tempFileName = "cxj-10.100.0.13";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream out = response.getOutputStream();
String contentTypeStr = request.getContentType();
int contentLen = request.getContentLength();
String contentType = request.getContentType();
String boundary = getBoundary(contentType);
ServletInputStream in = request.getInputStream();
FileOutputStream fou = null;
byte[] b = new byte[BUFFERSIZE];
int result;
try {
result = in.readLine(b,0,b.length); //读取boundary
result = in.readLine(b,0,b.length); //读取Content-Disposition
String upLoadFileName = getUpLoadFileName(new String(b,0,result));
fou = new FileOutputStream(tempFileName);
result = in.readLine(b,0,b.length); //读取Content-Type;
result = in.readLine(b,0,b.length); //读取空行;
int totalRead = 0;
result = in.readLine(b,0,b.length);
while((new String(b,0,result)).trim().indexOf(boundary) == -1) {
totalRead += result;
fou.write(b,0,result);
result = in.readLine(b,0,b.length);
}
out.println(totalRead);
fou.close();
in.close();
//处理文件
dealFile(upLoadFileName,totalRead);
} catch(Exception ex) {
System.out.print(ex.toString());
}
}
public void destroy(){}
/*
* 得到filename
*/
private String getUpLoadFileName(String line) {
int split = line.indexOf("filename=");
String tempFileName = delQuote(line.substring(split + 9,line.length()).trim());
if(tempFileName.indexOf("\") != -1) {
tempFileName = tempFileName.substring(tempFileName.lastIndexOf("\") + 1,tempFileName.length());
}
return tempFileName;
}
/*
* 得到分隔符
*/
private String getBoundary(String line) {
return line.substring(line.indexOf("boundary=") + 9,line.length()).trim();
}
/*
* 去除""
*/
private String delQuote(String line) {
if(line.indexOf(""") != -1) {
line = line.substring(1,(line.length() - 1));
}
return line;
}
/*
* 处理文件,把最后的两个字节删除
*/
private void dealFile(String fileName, int totalRead) throws IOException {
File file = new File(tempFileName);
byte[] b = new byte[totalRead - 2];
RandomAccessFile rafRead = new RandomAccessFile(file,"r");
rafRead.readFully(b,0,totalRead - 2);
rafRead.close();
file.delete();
RandomAccessFile rafWrite = new RandomAccessFile(fileName,"rw");
rafWrite.write(b);
rafWrite.close();
}
}
看看下面的上传文件的代码,你就知道了:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class UpLoadServlet extends HttpServlet {
private final String CONTENT_TYPE = "text/html;charset=gb2312";
private final int MACLEN = 2048 * 1024;
private final int BUFFERSIZE = 1024 * 8;
private final String tempFileName = "cxj-10.100.0.13";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream out = response.getOutputStream();
String contentTypeStr = request.getContentType();
int contentLen = request.getContentLength();
String contentType = request.getContentType();
String boundary = getBoundary(contentType);
ServletInputStream in = request.getInputStream();
FileOutputStream fou = null;
byte[] b = new byte[BUFFERSIZE];
int result;
try {
result = in.readLine(b,0,b.length); //读取boundary
result = in.readLine(b,0,b.length); //读取Content-Disposition
String upLoadFileName = getUpLoadFileName(new String(b,0,result));
fou = new FileOutputStream(tempFileName);
result = in.readLine(b,0,b.length); //读取Content-Type;
result = in.readLine(b,0,b.length); //读取空行;
int totalRead = 0;
result = in.readLine(b,0,b.length);
while((new String(b,0,result)).trim().indexOf(boundary) == -1) {
totalRead += result;
fou.write(b,0,result);
result = in.readLine(b,0,b.length);
}
out.println(totalRead);
fou.close();
in.close();
//处理文件
dealFile(upLoadFileName,totalRead);
} catch(Exception ex) {
System.out.print(ex.toString());
}
}
public void destroy(){}
/*
* 得到filename
*/
private String getUpLoadFileName(String line) {
int split = line.indexOf("filename=");
String tempFileName = delQuote(line.substring(split + 9,line.length()).trim());
if(tempFileName.indexOf("\") != -1) {
tempFileName = tempFileName.substring(tempFileName.lastIndexOf("\") + 1,tempFileName.length());
}
return tempFileName;
}
/*
* 得到分隔符
*/
private String getBoundary(String line) {
return line.substring(line.indexOf("boundary=") + 9,line.length()).trim();
}
/*
* 去除""
*/
private String delQuote(String line) {
if(line.indexOf(""") != -1) {
line = line.substring(1,(line.length() - 1));
}
return line;
}
/*
* 处理文件,把最后的两个字节删除
*/
private void dealFile(String fileName, int totalRead) throws IOException {
File file = new File(tempFileName);
byte[] b = new byte[totalRead - 2];
RandomAccessFile rafRead = new RandomAccessFile(file,"r");
rafRead.readFully(b,0,totalRead - 2);
rafRead.close();
file.delete();
RandomAccessFile rafWrite = new RandomAccessFile(fileName,"rw");
rafWrite.write(b);
rafWrite.close();
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。