2012-04-24 161 views
1

嗨我提交表单“java.io.IOException:损坏的表单数据:过早结束”时出现此错误,它发生时,我将此代码添加到现有scriptlet而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); 
} 
%> 

如果我排除在现有的代码,它的工作原理fine.some倍,如果我包括抓后的代码或者在try块的末尾它显示我null.i需要读取下拉列表的索引值并将它们与a连接点。错误是“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> 

回答

1

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

+0

我知道,但只有当我尝试读取下拉值时才会发生,否则它适用于所有类型的文件大小。 – ksa 2012-04-24 07:00:12

+0

当你有多重请求时,你不能使用'request.getParameter(“sel1”);'这是'HttpServletRequest'。使用'request2.getParameter(“sel1”);' – 2012-04-24 07:57:51

+0

它不能正常工作... – ksa 2012-04-24 08:27:58