上传文件后点击提交按钮获取异常IOE

时间:2017-07-06 19:04:29

标签: java jsp

这是我的JSP页面我上传后收到Exception并点击提交按钮
现在我应该如何处理JSP页面中的Exception。 java.io.IOException:在第41行处理JSP页面/upload.jsp时发生异常

upload.jsp

<%@page import="com.oreilly.servlet.*,java.sql.*,databaseconnection.*,java.util.*,java.io.*,javax.servlet.*, javax.servlet.http.*"%>
 <%

 String h1=null;
       String saveFile="";

String contentType = request.getContentType();

if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
 int ch;
StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;

String file = new String(dataBytes);

saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

fin = new FileInputStream(ff);


while ((ch = fin.read()) != -1)
strContent.append((char) ch);

fin.close();
String ss=(strContent.toString());
//StringBuffer s=new StringBuffer(ss);

String date=(String)session.getAttribute("date");

String keypoints=(String)session.getAttribute("keypoints");
/*
    String[] strArray =null;
    strArray=keypoints.split(",");
 for (String str : strArray) {
// String a="<a href>"+str+" </a>";
//String b="(?i)"+str;
h=ss.replaceAll(str,"<a href>"+str+" </a>");


 }
*/
String text=ss;
String result[]=text.split("\\s");

String[] strArray =keypoints.split(",");
    for(int i=0;i<strArray.length;i++)
    {
        for(int p=0;p<result.length;p++)
        {

            if(strArray[i].equals(result[p])){
            StringBuffer sb1=new StringBuffer();
            sb1.append("<a href=\"search.jsp?search="+strArray[i]+"\">"+strArray[i]+"</a>");
            result[p]=sb1.toString(); 

            }


        }


    }
    StringBuffer sb=new StringBuffer();
    for(int i=0;i<result.length;i++){
    sb.append(result[i]);
    sb.append(" ");
    }
    String ans=new String(sb);
    session.setAttribute("ans",ans);
out.println(ans);
String id=(String)session.getAttribute("id");

String filename=(String)session.getAttribute("filename");

//int n;
//session.getAttribute("userid", n );




String filesize=null;
String jj="no";

FileInputStream fis;

PreparedStatement psmt1=null;
try
{


Connection con = databasecon.getconnection();
psmt1=con.prepareStatement("insert into file(id,filename,date,file,ff,keypoints) values(?,?,?,?,?,?)");

//File f = new File(h);

psmt1.setString(1,id);
psmt1.setString(2,filename);
psmt1.setString(3,date);
//fis = new FileInputStream(f);


psmt1.setString(4,ans);
//double bytes = file.length();
//filesize=Double.toString(bytes);
psmt1.setString(5,jj);
//sout.println(filesize);
psmt1.setString(6,keypoints);


psmt1.executeUpdate();


response.sendRedirect("suc.jsp?success");


}
catch(Exception ex)
{

out.println("Error in connection : "+ex);

}


}

%>

这是例外:     =========================

 java.io.IOException: An exception occurred processing JSP page /upload.jsp at line 41

38: int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
39: System.out.println(saveFile);
40: File ff = new File(saveFile);
41: FileOutputStream fileOut = new FileOutputStream(ff);
42: 
43: fileOut.write(dataBytes, startPos, (endPos - startPos));
44: 


Stacktrace:
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2503)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2492)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: springframework-license.txt (Access is denied)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
    at org.apache.jsp.upload_jsp._jspService(upload_jsp.java:163)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    ... 27 more

1 个答案:

答案 0 :(得分:0)

您的堆栈跟踪说

  

&#34;引起:java.io.FileNotFoundException:springframework-license.txt   (访问被拒绝)&#34;

显然,对于用户tomcat(或您的应用程序)正在运行的文件springframework-license.txt本身或目录的访问被拒绝。

检查您的路径和文件访问权限。