简体   繁体   中英

org.apache.commons.fileupload.FileUploadException: Processing of multipart/form-data request failed

File upload program is working fine in the local pc when i deploy it online its giving an error, ie

org.apache.commons.fileupload.FileUploadException: Processing of multipart/form-data request failed. C:\\Program Files (x86)\\Apache Software Foundation\\Tomcat 5.0 \\upload_4133c5ef_14e0097e702__8000_00000003.tmp (The system cannot find the path specified)

Below is my code:

<%@page import="java.util.*"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.*"%>
<%@page import="javax.servlet.ServletConfig"%>
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%@page import="java.text.*"%>
<%@page import="javax.servlet.Servlet"%>
<%@page import="javax.servlet.ServletException"%>
<%@page import="javax.servlet.http.HttpServlet"%>
<%@page import="com.mmvisa.utils.ConnectionUtils"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Group file Upload</title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,700,600,800' rel='stylesheet' type='text/css' />
<link rel="stylesheet" href="../css/style.css" type="text/css" />

</head>
<body >
<div id="wrapper">

 <div id="header">

     <div id="logo">     
     <img src="../images/logo.jpg" alt="logo"  />     
     </div>    <!-- logo -->

     <div class="tittle">
      <h1> M M VISA-AID CONSULTANCY </h1>
       <p>OFFSHORE AGENT OF AUSTRALIAN GOVERNMENT <br />
Department of Immigration & Border Protection</p>

     </div>    <!-- tittle -->

  </div>   <!-- header -->


 <div class="content" style="min-height:420px;">




<br/>

<center>
<table border="2">

    <%
      ServletConfig config1 = getServletConfig();
  //    long  useridStr= 0;  
    String id = "";
    long compid = 0;

    //List<String> allfileName=new ArrayList<String>();
    String allfileName[]=new String[50];
    int counterNoOfFiles=0;
    int uid = 0;
    String year =null;
    String invalidFile=null;

      boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (!isMultipart){

    }else{

        List items = null;
        try{

            //FileItemFactory factory = new DiskFileItemFactory();

           // ServletFileUpload upload = new ServletFileUpload(factory);
           DiskFileUpload upload = new DiskFileUpload();
            items = upload.parseRequest(request);
        }catch (FileUploadException e){
            e.printStackTrace();

        }
        Iterator itr = items.iterator();
        while (itr.hasNext()) {

                FileItem item = (FileItem) itr.next();
                if (item.isFormField()){
                    ////

                    String name = item.getFieldName();
                    String value = item.getString();
                    Long value1 = item.getSize();
                    System.out.println("dj checking field name-->"+name);
                    if(name.equals("year")){
                        year=value;
                        System.out.println("dj checking field value-->"+year);
                    }
                }else   
                {

                    try{
                        String itemName = item.getName();
                        //allfileName.add(itemName);
                        if (item.getSize() > 0){
                            File uploadedFile = null; 
                            String myFullFileName = item.getName(), myFileName = "",
                            slashType = (myFullFileName.lastIndexOf("\\") > 0) ? "\\" : "/"; // Windows or UNIX
                            //System.out.println("in multiple file upload"+slashType);
                            int startIndex = myFullFileName.lastIndexOf(slashType);
                            myFileName = myFullFileName.substring(startIndex + 1, myFullFileName.length()); 
                            System.out.println("in multiple file upload"+myFileName);
                            //uploadedFile = new File("C:\\Documents and Settings\\sys\\Desktop\\shastrogyan\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\Hayagriva\\files\\", myFileName);
                            uploadedFile = new File(getServletContext().getRealPath("\\")+"clientfeedback", myFileName);
                            //getServletContext().getRealPath("\\")+"uploadfiles\\"
                            System.out.println("uploaded file "+uploadedFile);
                            item.write(uploadedFile);
                            allfileName[counterNoOfFiles]=itemName;
                            counterNoOfFiles++;
                        }  
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        }
    }
          Connection con = null;
          PreparedStatement preStat = null;
          ResultSet rs = null;
          int count=0;
              String img=" ";
                        for(int i=0;i<=counterNoOfFiles;i++)
                        {
                            System.out.println("Number of files "+counterNoOfFiles);
                            img = allfileName[i];
                            System.out.println("Image name "+img);
                        }
                        System.out.println("All Image names "+img);

            //Code for Add to database
            for(int i=0;i<counterNoOfFiles;i++){
                //System.out.println("175"+allfileName.size());

                PreparedStatement psmt=null;

                try{
                  con=ConnectionUtils.getConnection();
                String sql = "select * from clientfeedback where imagename=?";
                System.out.println(sql);
                psmt=con.prepareStatement(sql);
                psmt.setString(1, allfileName[i].toString());
                rs=psmt.executeQuery();
                if(rs.next()){
                    count++;
                }else{
                    if (psmt != null)
                        psmt.close();
                  String query = "insert into clientfeedback (imagename,year ) values(?,?)";

                System.out.println(query);
                        psmt=con.prepareStatement(query);
                        psmt.setString(1, allfileName[i].toString());
                        psmt.setString(2,year);
                        psmt.executeUpdate();
                }
                    }catch(Exception ex){
                        ex.printStackTrace();
                    }finally{

                        try {
                            if (psmt != null)
                                psmt.close();
                        } catch (Exception e2) {
                        }
                        try {
                            if (con != null)
                                con.close();
                        } catch (Exception e3) {
                        }
                }
            }
            System.out.println("checking count->"+count);
            if(count!=0){
                invalidFile="there is "+count+" duplicate file which is not save in database.other file has saved"; 
            }else{
            invalidFile="File uploading done successfully"; 
            }
   %>

</table>
</center>
<center>
<h3><%=invalidFile %></h3>
</center>
<center><a href="<%=request.getContextPath() %>/clientFeedback.do?action=list">Go to upload page</a></center>
<br/>

</div>  <!-- content -->

<div id="footer" style="margin-top:10px;">
       <table>
       <tr>
       <td width="900">Copyright, All Rights Reserved,MM VISA,2014</td>
       <td width="460">Follow us on : <a href="#"> <img src="../images/facebook.png" /> </a> <a href="#"> <img src="../images/twitter.png" /> </a>  <a href="#"> <img src="../images/utube.png" /> </a>`enter code here`
       <a href="#"> <img src="../images/blog1.png" /></a>
       <a href="#"> <img src="../images/google_plus_icon.png" /> </a>

        </td>
      </tr>
        </table>
        </div>  <!-- footer-->

</div>  <!-- wrapper -->
</body>
</html>

Could anyone please help me to solve this problem. Thank you

Apache Commons FileUpload uses the path as set in the java.io.tmpdir system/environment variable as default upload path. You can change the default upload path. Try to see if it solves the problem. Check their docs for configuration details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM