【问题标题】:java.io.IOException: Corrupt form data: premature endingjava.io.IOException:损坏的表单数据:提前结束
【发布时间】:2012-05-04 19:11:12
【问题描述】:

您好,当我提交表单“java.io.IOException:损坏的表单数据:过早结束”时出现此错误,当我在现有脚本中添加此代码时会发生此错误

String selectedValue=request.getParameter("sel1");
//out.println("Selected Value is: "+selectedValue);

String select1=request.getParameter("sel2");
//out.println("selected values is:"+select1);

String concat=selectedValue+"." +select1;
out.println(""+concat);

我现有的脚本

<%@ page import="java.io.*,java.sql.*,java.util.zip.*,com.oreilly.servlet.*" %>
<%
try
 {    
   //if i include here,i can retrive the values but i cant upload the file into database,shows me "java.io.IOException: Corrupt form data: premature ending "
 String selectedValue=request.getParameter("sel1");
//out.println("Selected Value is: "+selectedValue);

String select1=request.getParameter("sel2");
//out.println("selected values is:"+select1);

String concat=selectedValue+"." +select1;
out.println(""+concat);

Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/ksa";
PreparedStatement psmnt = null;
    MultipartRequest request2=new MultipartRequest(request,"/home/adapco/Desktop/output",1024*1024*1024);
String filename=request2.getFilesystemName("file");

File f=request2.getFile("file");
out.println(f.exists()+"----------------"+f.getAbsolutePath());
out.print(filename);
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
psmnt = connection.prepareStatement("insert into file1(file_path) values(?)");
psmnt.setString(1, f.getPath());
int s = psmnt.executeUpdate();

 //if i include here it shows me null.null

String selectedValue=request.getParameter("sel1");
//out.println("Selected Value is: "+selectedValue);

String select1=request.getParameter("sel2");
//out.println("selected values is:"+select1);

String concat=selectedValue+"." +select1;
out.println(""+concat);

if(s>0)
{
 System.out.println("Uploaded successfully !");
}
else
{
 System.out.println("Error!");
}
}
catch(Exception e)
{
  out.print("-----------error--------------"+e);
}
%>

如果我从现有代码中排除代码,它可以正常工作。有时如果我在 catch 之后或 try 块的末尾包含代码,它显示我为 null。我需要读取下拉列表的索引值并用一个点连接它们。错误是“null.null”。实际结果应该是示例:1.1。 这是我的html代码

<%@ page language="java" %>
<HTML>
   <FORM ENCTYPE="multipart/form-data" ACTION="uploadFile.jsp" METHOD=POST>
    <center>
   <table bgcolor=#38ACEC>
    <tr>
    <center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
     </tr>
    <tr><td colspan="2" align="center"> </td></tr>
    <tr><td><b>Choose the file To Upload:</b></td>
    <td><INPUT NAME="file" TYPE="file"></td>
    </tr>
    <tr><td><select name="sel1">
     <option value="1">Aerospace</option>
     <option value="2">Automotive</option>
     <option value="3">Energy</option>
     <option value="4">IC Engines</option>
     <option value="5">Wind</option>
       <option value="6">Turbo</option>
      <option value="7">IT</option>
      <option value="8">Training</option>
     </select>
     <br>
     <select name="sel2">
     <option value="1">Internal</option>
     <option value="2">Demos</option>
     <option value="3">Best Practice</option>
       <option value="4">Marketing</option>
     <option value="5">Papers & public</option>
     <option value="6">Validation</option>
     <option value="7">Training</option>
      </select></td></tr>
      <tr><td colspan="2" align="center"> </td></tr>
      <tr><td colspan="2" align="center"><input type="submit" value="Send File"> </td></tr>
     <table>
   </center>
    </FORM>
   </html>  

【问题讨论】:

    标签: mysql html api jsp


    【解决方案1】:

    这是一个非常古老的例子。Oreilly 多部分解析器MultipartRequest 有一些错误。有关概述,请参阅相关问题Corrupt form data: premature ending (Resolved)。而是使用Apache Commons FileUpload 或仅使用request.getPart() 方法中内置的新Servlet 3.0。另见How to upload files in JSP/Servlet?

    【讨论】:

    • 我知道,但只有当我尝试读取下拉值时才会发生这种情况,否则它适用于所有类型的文件大小。
    • 当你有多部分请求时,你不能使用request.getParameter("sel1");,它是'HttpServletRequest'。使用request2.getParameter("sel1");
    • 我用request2.getParameter("sel1"); 尝试了你的同一个例子,它工作并产生了选定的值。因此,再次检查并提供正确的结果,而不是“它也不起作用..”。你应该总是解释正在发生的事情。
    • 哦,对不起,我试图修改并忘记将 id 更改为 name....它现在可以工作了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2018-05-22
    • 2011-04-20
    • 1970-01-01
    相关资源
    最近更新 更多