本帖最后由 minfogn 于 2016-9-7 23:59 编辑
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="java.io.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<center>在线投稿</center><hr>
<form method="post" action="acceptFile.jsp" enctype="multipart/form-data">
姓名:<input type="text" name="name"><br>
电话:<input type="text" name="phone"><br>
Email:<input type="text" name="email"><br>
稿件:<input type="file" name="filename"><br>
<input type="submit" name="Submit" value="提交">
</form>
</table>
</body>
</html>
acceptFile.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>在线投稿</title>
</head>
<body>
<%
String strTempfile="paper"+(int)(Math.random()*10000);
File fTempfile=new File("c:/test/",strTempfile);
FileOutputStream outStream=new FileOutputStream(fTempfile);
InputStream inStream=request.getInputStream();
byte b[]=new byte[10000];
int n;
while((n=inStream.read(b))!=-1){
outStream.write(b,0,n);
}
outStream.close();
inStream.close();
%>
<%
String strName=null;
String strPhone=null;
String strEmail=null;
String strFilename=null;
RandomAccessFile rf=new RandomAccessFile(fTempfile,"r");
int intLine=1;
String strLine=null;
String Filename=null;
while(intLine<=16){
strLine=rf.readLine();
if(intLine==4){
strName=strLine;
}
if(intLine==8){
strPhone=strLine;
}
if(intLine==12){
strEmail=strLine;
}
intLine++;
}
%>
<%
if(intLine==14){
int position=strLine.lastIndexOf("filename");
Filename=strLine.substring(position+10,strLine.length()-1);
}
%>
<%
fTempfile.delete();
%>
<%
long startPoint =rf.getFilePointer();
File f2=new File("c:/test/submitfile",Filename);
RandomAccessFile rf2=new RandomAccessFile(f2,"rw");
rf.seek(rf.length());
long endPosition=rf.getFilePointer();
long mark=endPosition;
int j=1;
while((mark>=0)&&(j<=6)){
mark--;
rf.seek(mark);
n=rf.readByte();
if(n=='\n'){
endPosition=rf.getFilePointer();
j++;
}
}
rf.seek(startPoint);
while(startPoint<endPosition-1){
n=rf.readByte();
rf2.write(n);
startPoint=rf.getFilePointer();
}
rf2.close();
rf.close();
%>
<%
fTempfile.delete();
out.print(Filename+"稿件已上传成功");
%>
<%
String strInfo=strTempfile+"#"+strFilename+"#"+strName+"#"+strPhone+"#"+strEmail;
RandomAccessFile rf3=new RandomAccessFile("c:/test/paperlist.txt","rw");
rf3.seek(rf3.length());
rf3.writeBytes(strInfo);
rf3.writeBytes("\n");
rf3.close();
%>
</body>
</html>
报错Servlet.service() for servlet [jsp] in context with path [/lbyTest4] threw exception [An exception occurred processing JSP page /acceptFile2.jsp at line 55
55行报错是 File f2=new File("c:/test/submitfile",Filename);
磁盘中c:/test/submitfile文件夹创了,上传不了,求解决
|
|