java上传问题,求各位高手帮帮忙,看看是什么问题,急!急!急!

时间:2023-01-26 19:39:07

Java代码如下:

      

package xiazai;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
public class upload extends HttpServlet {
private String uploadPath = "D://upload"; // 上传文件的目录
private String tempPath = "D://uploadtmp"; // 临时文件目录
 public void doPost1(HttpServletRequest request,
        HttpServletResponse response)throws IOException, ServletException
  {
  
  }

public void doPost(HttpServletRequest request,
  HttpServletResponse response)
  throws IOException, ServletException
  {
  try {
  DiskFileUpload fu = new DiskFileUpload();
  // 设置最大文件尺寸,这里是4MB
  fu.setSizeMax(4194304);
  // 设置缓冲区大小,这里是4kb
  fu.setSizeThreshold(4096);
  // 设置临时目录:
  fu.setRepositoryPath(tempPath);
  // 得到所有的文件:
  List fileItems = fu.parseRequest(request);
  Iterator i = fileItems.iterator();
  // 依次处理每一个文件:
  while(i.hasNext()) {
  FileItem fi = (FileItem)i.next();
  // 获得文件名,这个文件名包括路径:
  String fileName = fi.getName();
  // 在这里可以记录用户和文件信息
  // ...
  // 写入文件,暂定文件名为a.txt,可以从fileName中提取文件名:
  fi.write(new File(uploadPath + "a.txt"));
  }
  }
  catch(Exception e) {
  // 可以跳转出错页面
  }
  }
 
  public void init() throws ServletException {
  uploadPath = "D://图片";
  tempPath = "D://图片";
  // 文件夹不存在就自动创建:
  if(!new File(uploadPath).isDirectory())
   new File(uploadPath).mkdirs();
  if(!new File(tempPath).isDirectory())
   new File(tempPath).mkdirs();
  }
}

 

 

页面代码如下:

 

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
   <form action="fileupload" method="post"enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="submit" name="Submit" value="upload">
</form>
  
  </body>
</html>

 

 

web代码如下:

 

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">


<web-app>
<servlet>
 <servlet-name>upload</servlet-name>
 <servlet-class>upload</servlet-class>
</servlet>

<servlet-mapping>
 <servlet-name>upload</servlet-name>
 <url-pattern>/fileupload</url-pattern>
</servlet-mapping>
</web-app>

 

运行结果如下图:

 

 

 

页面结果:

type Status report

message Servlet upload is not available

 description The requested resource (Servlet upload is not available) is not available.

 

控制台结果:

 

严重: Error loading WebappClassLoader
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@cdedfd
 upload
java.lang.ClassNotFoundException: upload
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1358)
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
 at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1083)
 at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:806)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
 at java.lang.Thread.run(Unknown Source)
2011-3-15 15:17:20 org.apache.catalina.core.StandardWrapperValve invoke
严重: Allocate exception for servlet upload
java.lang.ClassNotFoundException: upload
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1358)
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
 at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1083)
 at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:806)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
 at java.lang.Thread.run(Unknown Source)