JSP文件上传老报错

minnnnmi 2008-09-09 10:24:26
1 <%@ page contentType="text/html;charset=GB2312"%>
2 <%@ page import="java.io.*"%>
3 <html>
4 <head><title>接受文件</title></head>
5 <body>
6 <%
7 try
8 {
9 InputStream in=request.getInputStream();
10 File f=new File( "D://Tomcat 5.5//webapps//ROOT//test","accept.txt");
11 FileOutputStream o=new FileOutputStream(f);
12 byte b[]=new byte[1000];
13 int n;
14 while(n=in.read(b)!=-1)
15 {
16 o.write(b,0,n);
17 }
18 o.close;
19 in.close;
20 }
21 catch(IOException e)
22 {
23 out.print("文件上传失败");
24 }
25 out.print("文件已经上传");
26 %>
27 </body>
28</html>
运行时老是报错,说第6行是无效的,不能编译。
...全文
82 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuheworld 2008-09-09
  • 打赏
  • 举报
回复


说明你的代码中间出现了异常 你可以输出一下你的异常看一下,我这里没有发现异常
JSP中 抛出异常 改动一下
catch(Exception e){
out.print(e);
}
异常应该就是在 while 以后 catch 以前吧 (猜测……)
wdbjsh 2008-09-09
  • 打赏
  • 举报
回复
文件上传失败文件已经上传---说明还是出了IO错误。。。。

out.print("文件上传失败");
修改为

e.printstachtrace();



看看到底是哪里出错鸟
minnnnmi 2008-09-09
  • 打赏
  • 举报
回复
能运行但是运行上传界面,上传文件是不管什么最后显示是 “文件上传失败文件已经上传” 我的令一个界面代码是

<%@ page contentType="text/html;charset=GB2312"%>
<%@ page import="java.io.*"%>
<html>
<head><title>文件的上传</title></head>

<body>
<p>选择你要上传的文件:<br>
<form action=".jsp" method="post" ENCTYPE="multipart/form-data">
<input type="File" name="accept" size="30">
<br>
<input type="submit" name="tijiao" value="提交">
<input type="reset" name="reset" value="重设">
</body>
</html>
minnnnmi 2008-09-09
  • 打赏
  • 举报
回复
刚刚弄错了,2楼给的可以运行了,2楼的能说下我是那出错了吗?
minnnnmi 2008-09-09
  • 打赏
  • 举报
回复
我是想把上传的的文件方到其他地方,并不是想吧他打印出来。还有2楼的我试过了,也不对。
liuheworld 2008-09-09
  • 打赏
  • 举报
回复
<%@ page contentType="text/html;charset=GB2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>接受文件</title>
</head>
<body>
<%
try {
InputStream in = request.getInputStream();
File f = new File(
"D:\\workspace\\apache-tomcat-5.5.26\\webapps\\",
"accept.txt");
FileOutputStream o = new FileOutputStream(f);
byte b[] = new byte[1000];
int n;
while ((n = in.read(b)) != -1) {
o.write(b, 0, n);
}
o.close();
in.close();
} catch (IOException e) {
out.print("文件上传失败");
}

out.print("文件已经上传");
%>
</body>
</html>
tangwq121 2008-09-09
  • 打赏
  • 举报
回复

<%
InputStream is = request.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(is));

String buffer = null;

while((buffer = br.readLine()) != null)
{
out.print(buffer + "<br>");
}
%>

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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