jsp上传文件时读取文件出错 本帖最后由 lokichao 于 2009-08-08 16:54:09 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 request.getInputStream(); 你真有趣,在jsp里面还想处理输入流? upfile.jsp<%@ page contentType="text/html;charset=gb2312" %><%@ page import="java.io.*" %><html><body> <% String tempfilename=(String)session.getId(); File f1=new File("D:\\JAVA\\Tomcat\\webapps\\fileup",tempfilename); int n; try { InputStream in=request.getInputStream(); BufferedInputStream my_in=new BufferedInputStream(in); FileOutputStream fout=new FileOutputStream(f1); BufferedOutputStream my_out=new BufferedOutputStream(fout); byte[] b=new byte[10000]; while((n=my_in.read(b))!=-1) { my_out.write(b,0,n); } my_out.flush(); my_out.close(); fout.close(); my_in.close(); in.close(); out.print("文件上传成功!<br>"); } catch(IOException e) { out.print("文件上传失败!"); } try { RandomAccessFile random1=new RandomAccessFile(f1,"r"); random1.readLine(); String filename=random1.readLine(); byte[] b=filename.getBytes("ISO-8859-1"); filename=new String(b); int pointer=filename.lastIndexOf('\\'); filename=filename.substring(pointer+1,filename.length()-1); File f2=new File("J:\\Tomcat 5.5\\webapps\\ROOT\\test",filename); RandomAccessFile random2=new RandomAccessFile(f2,"rw"); random1.seek(0); for(int i=1; i<=4; i++) { String tempstr=random1.readLine(); } long startPoint=random1.getFilePointer(); random1.seek(random1.length()); long =random1.getFilePointer(); int j=0; long endPoint=0; while((>=0)&&(j<=5)) { --; random1.seek(); n=random1.readByte(); if(n=='\n') { j++; endPoint=random1.getFilePointer(); } } long length=endPoint-startPoint+1; int order=(int)(length/10000); int left=(int)(length%10000); byte[] c=new byte[10000]; random1.seek(startPoint); for(int i=0; i<order; i++) { random1.read(c); random2.write(c); } random1.read(c,0,left); random2.write(c,0,left); random1.close(); random2.close(); f1.delete(); out.print("文件更新成功!"); } catch(Exception e) { out.print("文件更新失败!"); } %></body></html> uploadBean.java/* * Created on 2009-8-8 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */package MyBean;/** * @author innolux_08 * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */import java.io.*;import javax.servlet.http.HttpServletRequest;public class uploadBean { public void doUpload(HttpServletRequest request) throws IOException { //String filename=(String)session.getId(); File tempf=new File("D:\\JAVA\\Tomcat\\webapps\\fileup","2"); int n; try { InputStream in=request.getInputStream(); BufferedInputStream my_in=new BufferedInputStream(in); FileOutputStream fout=new FileOutputStream(tempf); BufferedOutputStream my_out=new BufferedOutputStream(fout); byte[] b=new byte[10000]; while((n=my_in.read(b))!=-1) { my_out.write(b,0,n); } my_out.flush(); my_out.close(); fout.close(); my_in.close(); in.close(); //out.print("文件上传成功!<br>"); } catch(IOException e) { //out.print("文件上传失败!"); } }} http://download.csdn.net/source/1532774上面有完整的例子 楼主你这个代码也叫上传文件啊网页中上传文件的时候HttpInputStream 里有HTTP请求头的信息,和文件的内容并不只有单一的文件内容,所以你要在服务器段对HttpInputStream 进行解析才能获取到文件的内容楼主你就直接用那个cos 上传文件的组件包就可以了顺便可以看看里面的源代码 <s:if test>判断问题 新手求助 JSP连接数据库显示的地方该怎么写 表单验证,复选框至少选一个,并验证其它的字段 关于Cookie的一点小问题 JBuilder工程在外部TomCat里发布,运行问题 为什么页面显示不出来? 第一次来这个版,现在想转学jsp,怎么开始啊? sjp中提交、删除数据 一个非常可爱的问题:-) 网站发送的手机验证码,为什么有失效的时间,意义在哪里? derby数据库调用一直提示这样的错误,不知道是怎么回事 修改不了数据库表中的记录呀
你真有趣,在jsp里面还想处理输入流?
<%@ page import="java.io.*" %>
<html>
<body>
<% String tempfilename=(String)session.getId();
File f1=new File("D:\\JAVA\\Tomcat\\webapps\\fileup",tempfilename);
int n;
try
{
InputStream in=request.getInputStream();
BufferedInputStream my_in=new BufferedInputStream(in);
FileOutputStream fout=new FileOutputStream(f1);
BufferedOutputStream my_out=new BufferedOutputStream(fout);
byte[] b=new byte[10000];
while((n=my_in.read(b))!=-1)
{
my_out.write(b,0,n);
}
my_out.flush();
my_out.close();
fout.close();
my_in.close();
in.close();
out.print("文件上传成功!<br>");
}
catch(IOException e)
{
out.print("文件上传失败!");
}
try
{
RandomAccessFile random1=new RandomAccessFile(f1,"r");
random1.readLine();
String filename=random1.readLine();
byte[] b=filename.getBytes("ISO-8859-1");
filename=new String(b);
int pointer=filename.lastIndexOf('\\');
filename=filename.substring(pointer+1,filename.length()-1);
File f2=new File("J:\\Tomcat 5.5\\webapps\\ROOT\\test",filename);
RandomAccessFile random2=new RandomAccessFile(f2,"rw");
random1.seek(0);
for(int i=1; i<=4; i++)
{
String tempstr=random1.readLine();
}
long startPoint=random1.getFilePointer();
random1.seek(random1.length());
long =random1.getFilePointer();
int j=0;
long endPoint=0;
while((>=0)&&(j<=5))
{
--;
random1.seek();
n=random1.readByte();
if(n=='\n')
{
j++;
endPoint=random1.getFilePointer();
}
}
long length=endPoint-startPoint+1;
int order=(int)(length/10000);
int left=(int)(length%10000);
byte[] c=new byte[10000];
random1.seek(startPoint);
for(int i=0; i<order; i++)
{
random1.read(c);
random2.write(c);
}
random1.read(c,0,left);
random2.write(c,0,left);
random1.close();
random2.close();
f1.delete();
out.print("文件更新成功!");
}
catch(Exception e)
{
out.print("文件更新失败!");
}
%>
</body>
</html>
* Created on 2009-8-8
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package MyBean;/**
* @author innolux_08
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/import java.io.*;
import javax.servlet.http.HttpServletRequest;
public class uploadBean {
public void doUpload(HttpServletRequest request) throws
IOException {
//String filename=(String)session.getId();
File tempf=new File("D:\\JAVA\\Tomcat\\webapps\\fileup","2");
int n;
try
{
InputStream in=request.getInputStream();
BufferedInputStream my_in=new BufferedInputStream(in);
FileOutputStream fout=new FileOutputStream(tempf);
BufferedOutputStream my_out=new BufferedOutputStream(fout);
byte[] b=new byte[10000];
while((n=my_in.read(b))!=-1)
{
my_out.write(b,0,n);
}
my_out.flush();
my_out.close();
fout.close();
my_in.close();
in.close();
//out.print("文件上传成功!<br>");
}
catch(IOException e)
{
//out.print("文件上传失败!");
}
}}
上面有完整的例子
网页中上传文件的时候HttpInputStream 里有HTTP请求头的信息,和文件的内容
并不只有单一的文件内容,所以你要在服务器段对HttpInputStream 进行解析才能
获取到文件的内容
楼主你就直接用那个cos 上传文件的组件包就可以了
顺便可以看看里面的源代码