How to upload a file to server?
[nobr]How to upload a file to server?
I tried to do a sample application with the help of
1) http://jakarta.apache.org/commons/fileupload/
2) http://jakarta.apache.org/commons/fileupload/using.html
My code:
1) index.jsp
<html>
<head>
<title>File Upload</title>
<body>
<FORM ENCTYPE='multipart/form-data' method='POST' action='fileUpload.jsp'>
<INPUT TYPE='file' NAME='file1'>
<INPUT TYPE='submit' VALUE='Upload'>
</FORM>
</body>
</html>
2) fileUpload.jsp
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.File"%>
<html>
<head>
<title>File Upload</title>
<%
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// Create a factory for disk-based file items
FileItemFactory factory =new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload =new ServletFileUpload(factory);
// Parse the request
List items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()){
FileItem item = (FileItem) iter.next();
if (!item.isFormField()){
out.println("item.getFieldName() = " + item.getFieldName());%><br><%
out.println("item.getName() = " + item.getName());%><br><%
out.println("item.getString() = " + item.getString());%><br><%
out.println("item.getContentType() = " + item.getContentType());%><br><%
out.println("item.isInMemory() = " + item.isInMemory());%><br><%
out.println("item.getSize() = " + item.getSize());%><br><br><%
File uploadedFile =new File("C:\\Tmp.txt");//Working fine
File uploadedFile =new File("http://www.geocities.com/mysite/");//Not working
item.write(uploadedFile);
}
}
%>
<body>Uploaded Successfully!</body>
</html>
When I try to upload to my system, its working perfectly. My doubt is whether I can upload it directly to an internet site.
Ex:- I need to upload the selected file to my geocity site.
If its possible, pls guide me how to do that in an easiest way
Thanks[/nobr]

