You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Leunamal <al...@gmail.com> on 2009/04/03 19:15:44 UTC

Problem uploading files with FileUpload and HttpClient

Hi, I'm trying to upload a file, but I dont achieve it. 

I'm using a class that extends from JApplet. When I execute the applet in my
apache server, I have this message:
File Length = 20
statusLine>>>HTTP/1.1 200 OK

But file, isn't uploaded.

This is the source code of my applet:
--------------------------------------------------
package multi;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.swing.JApplet;

public class PostAFile extends JApplet{
	private static final long serialVersionUID = -514976829358415881L;
	private static String url
="http://localhost:8080/multi-jsp/ProcessFileUpload.jsp";

	@SuppressWarnings("deprecation")
	public void init(){
		HttpClient client = new HttpClient();
		PostMethod postMethod = new PostMethod(url);
        client.setConnectionTimeout(8000);

        File f = new File("D://Documentos//messages.properties");
        System.out.println("File Length = " + f.length());
        
        FileInputStream fis = null;
		try {
			fis = new FileInputStream(f);
		} catch (FileNotFoundException exc) {
			// TODO Auto-generated catch block
			exc.printStackTrace();
		}
        postMethod.setRequestBody(fis);
        postMethod.setRequestHeader("Content-type",
            "text/html;charset=windows-1252");

        try {
        	client.executeMethod(postMethod);
        	
		} catch (Exception exce) {
			// TODO Auto-generated catch block
			exce.printStackTrace();
		}
        System.out.println("statusLine>>>" + postMethod.getStatusLine());
        postMethod.releaseConnection();
    }//Fin de init().
}

-----------------------------------------------------------------

The file ProcessFileUpload.jsp is this:
-----------------------------------------------------------------------------------
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="org.apache.commons.fileupload.FileItemStream"%>
<%@ page import="org.apache.commons.fileupload.FileItemIterator"%>
<%@ page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="java.io.InputStream"%>
<%@ page import="java.io.FileOutputStream"%>

<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>
<%
		try{
		// Create a factory for disk-based file items
        DiskFileItemFactory  factory = new DiskFileItemFactory();
		ServletFileUpload fu = new ServletFileUpload(factory);
        fu.setSizeMax(1000000);
        FileItemIterator iterator = fu.getItemIterator(request);
        FileItemStream item = iterator.next();
		InputStream stream = item.openStream(); 
        File fNew= new File(application.getRealPath("/"),item.getName());
		FileOutputStream outputstream = new FileOutputStream(fNew);
		}catch(Exception e){
			e.printStackTrace();
		}
%>
-----------------------------------------------------------------------------------

I don't find the problem. Could anyone help me please?

Thanks in advance.
-- 
View this message in context: http://www.nabble.com/Problem-uploading-files-with-FileUpload-and-HttpClient-tp22872589p22872589.html
Sent from the Commons - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org