使用带有Multipartrequest的Java Servlet上传图像

时间:2013-01-20 05:51:32

标签: jsp servlets image-uploading multipartform-data

 package imageUplaod;

    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import com.oreilly.servlet.MultipartRequest;
    import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
    import java.util.Enumeration;



     public class UploadImage extends HttpServlet {
       private static final long serialVersionUID = 1L;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {        

    final int permitedSize = 314572800;  //~ 300 MB in bytes

    PrintWriter out = response.getWriter();
    response.setContentType("text/html;charset=UTF-8");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "No-cache");
    response.setDateHeader("Expires",0);                       

    try{               
       String type = "";
       String name = "";
       String originalFilename = "";
       String extension1 = "";
       String extension2 = "";
       String filename = "";

       String strDirectory = "files";
       ServletContext sc=getServletContext();
       String uploadPath = sc.getRealPath("//WEB-INF//"+strDirectory+"//");

       MultipartRequest multipartRequest = new MultipartRequest(request, uploadPath, permitedSize, "ISO-8859-1", new DefaultFileRenamePolicy()); 

       Enumeration<String> files = multipartRequest.getFileNames();           

          while (files.hasMoreElements()) 
             { 
                 name = files.nextElement();
                 type = multipartRequest.getContentType(name); 
                 filename = multipartRequest.getFilesystemName(name); 
                 originalFilename = multipartRequest.getOriginalFileName(name);                 

                 extension1 = filename.substring(filename.length() - 4, filename.length());
                 extension2 = originalFilename.substring(originalFilename.length() - 4, originalFilename.length());

                 //return a File object for the specified uploaded file                   
                 File currentFile = multipartRequest.getFile(name);
                 InputStream inputStream = new BufferedInputStream(new FileInputStream(currentFile));
                 if(currentFile == null) {
                      out.println("There is no file selected!");
                      return;
                    }

                 //checking the file extensions according to the MIME types
                 if((type.equals("text/plain") || type.equals("image/gif") || type.equals("image/pjpeg")
                   || type.equals("image/x-png") || type.equals("image/jpeg"))) {       
                     //show a few details about the uploaded file        
                     out.println("file: "+filename);
                     out.println("type: "+type); 
                     out.println("size: "+currentFile.length());
                   } else {
                            out.println("\n Invalid format!");
                          } 
                 }                        

       }catch (Exception exception)
          {                     
      response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
      } finally { if(out != null) {out.close();} }      
    }       


    public UploadImage() {
        super();
        // TODO Auto-generated constructor stub
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);

    }
    }

       this is my index page:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@page
    language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <html>
    <head>
    <title>index</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    </head>
    <body>

    <h5>O'Reilly COS Upload</h5>
    <table>
    <tr>
     <td align="center">
      <form enctype="multipart/form-data" action="UploadImage" method="POST">
       Select the file to upload: 
       <input type="file" name="userfile">
       <input type="submit" value="Upload">
    </form>
    </td>
    </tr>
   </table>
   </body>
   </html>

当我运行它时出现以下错误:

      Error 404: javax.servlet.UnavailableException: SRVE0203E: Servlet [UploadImage]:  
     imageUplaod.UploadImage was found, but is missing another required class. SRVE0206E: 

此错误通常意味着servlet最初使用服务器无法定位的类进行编译。 SRVE0187E:检查类路径以确保servlet所需的所有类都存在.SRVE0210I:可以通过仅使用应用程序的运行时类路径SRVE0234I中的类重新编译servlet来调试此问题:

  Application class path=[C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\java\lib;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\java\lib\dt.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\java\lib\htmlconverter.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\java\lib\ibmorbtools.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\java\lib\jconsole.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\java\lib\tools.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\classes;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\classes;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\EJBCommandTarget.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\IVTClient.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\OTiSConvertTime.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\activation-impl.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\aspectjrt.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\bootstrap.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\bsf-engines.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\cimgr.ops.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\commandlineutils.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\commons-discovery.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\databeans.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\ffdcSupport.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\htmlshell.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\installver.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\installxml.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\iscdeploy.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\ivblogbr.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\j2ee.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\jNative2ascii.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\jacl.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\jrom.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\jsf-nls.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\launchclient.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\lmproxy.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\mail-impl.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\nif.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\pc-appext.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\pmirm4arm.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\rrd-appext.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\rsadbutils.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\rsahelpers.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\serviceadapter.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\setup.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\startup.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\tcljava.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\urlprotocols.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\wses_dynaedge.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\wsif-compatb.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\installedChannels;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\web\help;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\deploytool\itp\plugins\com.ibm.etools.ejbdeploy\runtime;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\deploytool\itp\plugins\com.ibm.etools.ejbdeploy\runtime\batch.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\deploytool\itp\plugins\com.ibm.etools.ejbdeploy\runtime\ejbdeploy.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\deploytool\itp\plugins\com.ibm.etools.ejbdeploy\runtime\ejbmapvalidate.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\derby\lib\derby.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\installedConnectors\sib.api.jmsra.rar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.commonservices.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.connector.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.headers.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jmqi.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jmqi.local.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jmqi.remote.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jmqi.system.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jms.admin.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.pcf.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mqjms.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.commonservices.j2se.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.commonservices.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.jms.internal.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.jms.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.matchspace.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.provider.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.ref.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.wmq.common.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.wmq.factories.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.wmq.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.wmq.v6.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\dhbcore.jar;C:\Program Files (x86)\IBM\SQLLIB\java\db2jcc.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\universalDriver\lib\db2jcc_license_cu.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1/properties;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/properties;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/lib/startup.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/lib/bootstrap.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/lib/jsf-nls.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/lib/lmproxy.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/lib/urlprotocols.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/deploytool/itp/batchboot.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/deploytool/itp/batch2.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/java/lib/tools.jar;C:\Users\drunkendeath\IBM\rationalsdp\workspace\UploadImage\WebContent\WEB-INF\classes;C:\Users\drunkendeath\IBM\rationalsdp\workspace\UploadImage\WebContent]

所以这里有什么问题,任何人都可以解释一下吗? 我已经包含了所有.jars。代码中没有错误。

1 个答案:

答案 0 :(得分:1)

错误告诉您Websphere未找到您的某个导入。您使用的唯一类是O'Reilly和标准Java。它不太可能是未找到的Java类之一。

确保O'Reilly jar位于WEB-INF / lib目录中或将其放入Websphere的类路径中。如果它存在,请重新启动Websphere(标准修复我在IBM时所使用的产品中的解决方案。)