0

データベースに画像ファイルをアップロードしようとしています。コードを実行すると、エラーが発生しますjava.lang.IndexOutOfBoundsException:。しかし、同じコードは別の形式でもうまく機能します。

私のコードは次のとおりです。

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

    PrintWriter out = response.getWriter();
    String uname = (String) request.getSession().getAttribute("uname");
    //set content type
    response.setContentType("text/html");

    DiskFileItemFactory  fileItemFactory = new DiskFileItemFactory ();

     fileItemFactory.setSizeThreshold(1*1024*1024); //1 MB

    ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
    try {
            if (! ServletFileUpload.isMultipartContent(request)) {
            System.out.println("sorry. No file uploaded");
            return;
        }

        // parse request
        List<?> items = uploadHandler.parseRequest(request);  //getting error
        System.out.println(items.size());
         // get uploaded file
        FileItem file = (FileItem) items.get(0);

                Connection con=prepareConnection();
                con.setAutoCommit(false);

                 //Part photo=request.getPart("photo");
                BufferedImage originalImage=ImageIO.read(file.getInputStream());
                int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
                BufferedImage resizeImageJpg = resizeImage(originalImage, type);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ImageIO.write(resizeImageJpg, "png", baos);
                baos.flush();
                InputStream is = new ByteArrayInputStream(baos.toByteArray());

                StringBuilder sb=new StringBuilder(1024);
                sb.append("insert into ").append(uname).append("PROFILEPICTURE values(?,?)");
                String query = sb.toString();
                PreparedStatement ps = con.prepareStatement(query);
                ps.setInt(1, 1);
                ps.setBinaryStream(2, is);

                int i=ps.executeUpdate();
                con.commit();
                con.close();

                if(i==1)
                {
                RequestDispatcher dispatcher = request.getRequestDispatcher("/sp/profile");
                if(dispatcher != null) {
                    dispatcher.forward(request, response);
                }
                }
                else
                {
                    out.println("Error ocuured");
                }
            out.close();

        }
        catch(Exception ex) {
        ex.printStackTrace();
    }
}

コンソール出力:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at skypark.AddProfilePict.doPost(AddProfilePict.java:79)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

そして、私のフォームは次のとおりです。

<form id="form" method="post" action="AddProfilePict" enctype="multipart/form-data">
    <input type="file" accept="image/*" style="width: 450px; border:0px; background:none; margin-top:5px; width:245px;" value="">
<input class="upld" type="submit" value="Upload">
</form>

エラーの原因を特定できる人はいますか?

4

1 に答える 1

0

items実際には空でなければなりません。最初に長さを確認してから、次を使用してアイテムを取得してみてください

if(! items.isEmpty()) {
    FileItem file = (FileItem) items.get(0); 
}

また

if(items.size() > 0) {
    FileItem file = (FileItem) items.get(0); 
}
于 2013-02-26T17:04:54.327 に答える