只已知一个InputStream is = getStream();请问如何知道is究竟有多少size ? 如果是javamail中的得到附件的大小是用part.getSize().否则好象没有办法直接得到流的大小。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我想将这个流转换成文件存起来,但用如下的方法:byte[] b = new byte[1000];.....这样只能读取1000个字节,其他就读不了,怎么办? 哦,这样啊。看看下面的上传文件的代码,你就知道了: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(); }} 看错了,我是没用ServletInputStream,我这里只是用InputStream,不是在Servlet里面用的!如何用? java基础题 求解释 关于jar包的使用,求指教 求教 请教 -> Java 反射 struts2 spring hibernate 整合错误????? 菜鸟请教 一个结果不一定的莫名的问题 在线等 file类菜鸟问题,困扰了我好久 JTextPane有如下JTextArea有的类似的方法么? 哪里有Java 2D Graphics方面的介绍? (关于URL)为什么我这样写是错误的? 我是Java的初学者,有些问题请问? 有谁用“java web start”的時候還在客戶端安裝過其它文件??
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();
}}