Hey, I'm trying to upload a PDF file or XML file. The MultipartPostMethod is deprecated and I can't use PostMethod to post because my FileUpload won't accept application/pdf or text/xml
This is the PostFile
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.FileRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
public class PostAFile3 {
private static String url =
"http://localhost:8080/CDPReceive/CDPReceive";
public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
FileInputStream fis = new FileInputStream("C:\\Documents And Settings\\Dave\\Desktop\\Address_Change_Form.pdf");
//File f = new File("C:\\Documents And Settings\\Dave\\Desktop\\Address_Change_Form.pdf");
//System.out.println("File Length = " + f.length());
//FileRequestEntity fre = new FileRequestEntity(f, "application/pdf;");
int len = fis.available();
StringBuffer s = new StringBuffer();
for (int x = 1; x < len; x++) {
s.append((char) fis.read());
}
// Send any XML file as the body of the POST request
File f = new File("C:\\DOCROOT\\crossdomain.xml");
System.out.println("File Length = " + f.length());
FileRequestEntity fre = new FileRequestEntity(f, "text/xml; charset=ISO-8859-1");
postMethod.setRequestEntity(fre);
int statusCode1 = client.executeMethod(postMethod);
//String response = postMethod.getResponseBodyAsString();
System.out.println("statusLine>>>" + postMethod.getStatusLine() + " " + statusCode1);
postMethod.releaseConnection();
//System.out.println(response);
}
}
This is the Servlet to receive the request
package mybeans;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.fileupload.disk.*;
import java.util.*;
/**
* Servlet implementation class for Servlet: CDPReceive
*
*/
public class CDPReceive extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
/**
*
*/
private static final long serialVersionUID = -5977845925042928295L;
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public CDPReceive() {
super();
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
File fNew = null;
System.out.println("Doing post");
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
try{
// Parse the request
List /* FileItem */<?> items = upload.parseRequest(request);
Iterator<?> it = items.iterator();
while(it.hasNext()){
FileItem fi = (FileItem)it.next();
fNew = new File("C:\\Documents And Settings\\Dave\\"+fi.getName());
try{
fi.write(fNew);
}catch(Exception e){
e.printStackTrace();
}
}
}catch(FileUploadException fue){
fue.printStackTrace();
}
System.out.println("Done with post for ");
}
}