论坛首页 Java企业应用论坛

使用Webwork + Cos 在文件上传时候遇到的问题

浏览 6538 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-05-31  
使用webwork + cos 实现文件上传
在网上找到下面的例子,测试成功。可是我将file.jsp里面的java程序放到webwork action中去做,却遇到 java.io.IOException: Corrupt form data: premature ending 不知道该如何解决。

upload.jsp:

  <html>
<head>
<title>File Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
</head>
<body bgcolor="#FFFFFF" text="#000000"><p><font size="5"color="#FF0000">
 

<form name="Form1" enctype="multipart/form-data" method="post" action="file.jsp"> 
<p>上傳檔案 1: <input type="file"name="File1" size="20" maxlength="20"> </p>
<p>檔案1敘述: <input type="text" name="File1" size="30" maxlength="50"> </p>
<p>上傳檔案 2: <input type="file" name="File2" size="20" maxlength="20"> </p>
<p>檔案2敘述: <input type="text" name="File2" size="30" maxlength="50"> </p>
<p>上傳檔案3: <input type="file" name="File3" size="20" maxlength="20"> </p>
<p>檔案3敘述: <input type="text" name="File3" size="30" maxlength="50"> </p>
<p> <input type="submit"value="上傳"> <input type="reset" value="清除"> </p>
</form>

</body>
</html>



 


file.jsp:

<%@ page language="java" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>

<%! 
	// 宣告將上傳之檔案放置到伺服器的C:\Upload目錄中 
    // 宣告限制上傳之檔案大小為 5 MB 
    String saveDirectory = "C:\\Upload\\"; 
    int maxPostSize = 5 * 1024 * 1024 ; 
    int count=0;
    // 宣告敘述上傳檔案內容的變數,型態為String	
    String FileDescription[] = {null,null,null}; 

    // 宣告上傳檔案名稱 String FileName = null;	
    // 計算上傳檔案之個數 int count = 0 ;
%>

<html>
<head>
<title>File Upload</title>
</head>

<%	
    // 產一個新的MultipartRequest 的物件,multi 
    MultipartRequest multi = new MultipartRequest(request , saveDirectory , maxPostSize );; 
%>
<body>
<% 
	// 判斷是否有取得檔案敘述, 
	// 若有,將資料給FileDescription	
  	// 若無,將FileDescription的內容設為空白字串 
	
	if ( multi.getParameter("File1"); != null ); 
	{ 
		FileDescription[0] = multi.getParameter("File1");; 
	}
	else	
  	{ 
		FileDescription[0] = ""; 
	} 
	
	if ( multi.getParameter("File2"); != null ); 
	{ 
		FileDescription[1] = multi.getParameter("File2");; 
	} 
	else	
  	{ 
		FileDescription[1] = ""; 
	} 

	if ( multi.getParameter("File3"); != null ); 
	{ 
		FileDescription[2] = multi.getParameter("File3");; 
	} 
	else	
  	{ 
		FileDescription[2] = ""; 
	} 
	
	// 取得所有上傳之檔案輸入型態名稱 
	Enumeration filesname = multi.getFileNames();; 
	while (filesname.hasMoreElements();); 
	{ 
		String name = (String); filesname.nextElement();; 
		String FileName = multi.getFilesystemName(name);;	
  		File f = multi.getFile(name);; 
		String ContentType = multi.getContentType(name);;	
  
		if (FileName != null); 
		{ 
			count ++;

%>
<font color="red">你上傳的第<%= count %>個的檔案:</font><br>
檔案名稱為:<%= FileName %><br>
檔案型態為:<%=   ContentType %><br>
檔案的敘述:<%= FileDescription[count-1] %><br><br>

<%	
		 } // end if 
	} // end while
%> 
您總共上傳<font color="red"><%= count %></font>個檔案
</body>
</html>






action: 可是在new MultipartRequest  时候程序就throw java.io.IOException: Corrupt form data: premature ending 了。请问是什么原因造成的? 该如何解决


    // 宣告上傳檔案名稱 String FileName = null;	
	    // 計算上傳檔案之個數 int count = 0 ;

	    try {
			// 產一個新的MultipartRequest 的物件,multi 
			MultipartRequest multi = new MultipartRequest(request , saveDirectory , maxPostSize );;
		  
			
            //判斷是否有取得檔案敘述, 
			// 若有,將資料給FileDescription	
		  	// 若無,將FileDescription的內容設為空白字串 
			
			if ( multi.getParameter("File1"); != null ); 
			{ 
				FileDescription  = multi.getParameter("File1");; 
			}
			else	
		  	{ 
				FileDescription  = ""; 
			} 
			 
			 
			// 取得所有上傳之檔案輸入型態名稱 
			Enumeration filesname = multi.getFileNames();; 
			while (filesname.hasMoreElements();); 
			{ 
				String name = (String); filesname.nextElement();; 
				FileName = multi.getFilesystemName(name);;	
		  		File f = multi.getFile(name);; 
			    ContentType = multi.getContentType(name);;	
		  
				if (FileName != null); 
				{ 
					//count ++;

				}
			}
	    
	    
	    } catch (IOException e); {
			// TODO Auto-generated catch block
			e.printStackTrace();;
		} 

		
		
		
		
		
		
		return SUCCESS;
	}


   发表时间:2005-05-31  
有webwork就不需要用这种方式做了, 用UploadInterceptor代码会简洁很多很多......
0 请登录后投票
   发表时间:2005-05-31  
Readonly 写道
有webwork就不需要用这种方式做了, 用UploadInterceptor代码会简洁很多很多......


webwork.multipart.parser=cos
0 请登录后投票
   发表时间:2005-05-31  
建议你用pell,而不是cos,cos本来是带一个设置charset参数的,但是webwork封装的时候,去掉了这个charset参数(为了统一封装cos,pell,jakarta),造成的结果就是cos总是使用ISO8859-1charset,因此只要是中文文件名称就乱码。
0 请登录后投票
   发表时间:2005-06-01  
Readonly 写道
有webwork就不需要用这种方式做了, 用UploadInterceptor代码会简洁很多很多......


我本来想用webwork 的fileuploadinterceptor 的,可是不知道该怎么使用。

能给点指点吗?
0 请登录后投票
   发表时间:2005-06-01  
frogfool 写道

webwork.multipart.parser=cos



我将parser设置为cos了,还是会出现我上面提到的那个exception
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics