org.apache.commons.fileupload.FileUploadException:处理multipart / form-data请求失败

时间:2015-06-17 08:38:33

标签: java

当我在线部署文件上传程序在本地电脑上工作正常时会发出错误,即

  

org.apache.commons.fileupload.FileUploadException:处理   multipart / form-data请求失败。 C:\ Program Files(x86)\ Apache   Software Foundation \ Tomcat 5.0   \ upload_4133c5ef_14e0097e702__8000_00000003.tmp(系统不能   找到指定的路径)

以下是我的代码:

<%@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>

有谁可以帮我解决这个问题。谢谢

1 个答案:

答案 0 :(得分:0)

Apache Commons FileUpload使用java.io.tmpdir system/environment变量中设置的路径作为默认上传路径。您可以更改默认上载路径。试着看看它是否解决了这个问题。查看他们的文档以获取配置详细信息。