|
From: Ahmed A. <asa...@ya...> - 2007-05-18 10:00:29
|
Dear Art,
The below example succeeds, and file is saved at the server side (using Commons FileUpload):
>>I've disabled javascript in htmlunit because it can't seem to handle all the stuff we do.
Well, why don't you investigate the root cause, and submit a bug report.
To have others help, a test case make things much easier.
Many thanks,
Ahmed Ashour
-------------------------------------------
.java
HtmlPage page1 = (HtmlPage)client.getPage( "http://localhost/htmlunit/upload1.jsp" );
HtmlForm form = (HtmlForm)page1.getForms().get( 0 );
HtmlFileInput input = (HtmlFileInput)form.getInputByName( "image" );
input.setValueAttribute( "c:\\test.png" );
HtmlPage page2 = (HtmlPage)form.submit();
System.out.println( page2.asXml() );
-------------------------------------------
upload1.jsp
<html>
<head>
<title>Upload Information</title>
</head>
<body>
<div align="center">
<form enctype="multipart/form-data" action="upload2.jsp" method="POST" >
<table width="350">
<tr><td align="center"><h3>Submit information</h3></td></tr>
<tr><td>Name:</td></tr>
<tr><td><input name="name"/></td></tr>
<tr><td>CPR Number:</td></tr>
<tr><td><input name="CPR"/></td></tr>
<tr><td>Image:</td></tr>
<tr><td><input name="image" type="file"/></td></tr>
<tr><td> </td></tr>
<tr><td><input type="submit" value="Submit"/></td></tr>
</table>
</form>
</body>
-------------------------------------------
upload2.jsp
<%@page import="org.apache.commons.fileupload.*,org.apache.commons.fileupload.disk.*,java.util.*,java.io.*"%>
<%
if( request.getMethod().equals( "POST" )) {
System.out.println("entering post method in upload.jsp");
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(4096);
factory.setRepository( new File( application.getRealPath( "/images" ) ) );
FileUpload fu = new FileUpload(factory);
fu.setSizeMax(512000);
List fileItems = fu.parseRequest(request);
Iterator iter = fileItems.iterator();
String contextType = "";
String str = "";
long length = 123;
String message = "";
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if( !item.isFormField() ) {
System.out.println("item is a form field");
contextType = item.getContentType();
str = item.getName();
String fieldName = item.getFieldName();
length = item.getSize();
String fullPath = item.getName();
String filename = fullPath.substring( fullPath.lastIndexOf( "\\" ) + 1 ).toLowerCase();
String name = filename.substring( 0, filename.lastIndexOf( "." ) );
String extension = filename.substring( filename.lastIndexOf(".") + 1 );
if((filename.endsWith(".jpg") || filename.endsWith(".jpeg") || filename.endsWith(".gif") || filename.endsWith(".png")
|| filename.endsWith(".bmp")) && item.getContentType().contains("image")){
File f = new File( application.getRealPath( "/images/" + filename));
if(!f.exists()){
item.write( new File( application.getRealPath( "/images/" + filename ) ) );
message = "Write successful";
}
else{
boolean written = false;
for(int i=1;!written;i++){
//unique generated filename
String newFilename = name + i + "." + extension;
//unique generated filename including folder
String newPath = "/images/" + newFilename;
if(!new File( application.getRealPath( newPath ) ).exists() ){
item.write( new File( application.getRealPath( newPath ) ) );
written = true;
message = "file renamed to " + newFilename;
}
}
}
}
else message = "File is not a valid image";
%>
Message: <%=message%><br><br><hr>
Context: <%=contextType%><br>
Name: <%=str%><br>
Length: <%="" + length%><br><br><hr>
Original filename: <%=item.getName()%><br><br>
New filename: <%=filename %><br><br>
Name: <%=name%><br>
Extension: <%=extension%><br>
Field name: <%=fieldName%>
</div>
</body>
</html>
<%
}
else {
System.out.println( item.getFieldName() + '=' + item.getString() );
}
}
}
%>
____________________________________________________________________________________
Park yourself in front of a world of choices in alternative vehicles. Visit the Yahoo! Auto Green Center.
http://autos.yahoo.com/green_center/ |