First of all, why don't you just submit the form directly to that PHP script?
<form action="http://example.com/upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
If this is somehow not an option and you really need to submit the form to the servlet, then first create a HTML form like following in the JSP:
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
In the servlet which listens on an url-pattern
of /upload
, you have 2 options to handle the request, depending on what the PHP script takes.
If the PHP script takes the same parameters and can process the uploaded file the same way as the HTML form has instructed the servlet to do (I would still rather just let the form submit directly to the PHP script, but anyway), then you can let the servlet play for a transparent proxy which just transfers the bytes immediately from the HTTP request to the PHP script. The java.net.URLConnection
API is useful in this.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpURLConnection connection = (HttpURLConnection) new URL("http://example.com/upload.php").openConnection();
connection.setDoOutput(true); // POST.
connection.setRequestProperty("Content-Type", request.getHeader("Content-Type")); // This one is important! You may want to check other request headers and copy it as well.
// Set streaming mode, else HttpURLConnection will buffer everything in Java's memory.
int contentLength = request.getContentLength();
if (contentLength > -1) {
connection.setFixedLengthStreamingMode(contentLength);
} else {
connection.setChunkedStreamingMode(1024);
}
InputStream input = request.getInputStream();
OutputStream output = connection.getOutputStream();
byte[] buffer = new byte[1024]; // Uses only 1KB of memory!
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
output.close();
InputStream phpResponse = connection.getInputStream(); // Calling getInputStream() is important, it's lazily executed!
// Do your thing with the PHP response.
}
If the PHP script takes different or more parameters (again, I would rather just alter the HTML form accordingly so that it can directly submit to the PHP script), then you can use use Apache Commons FileUpload to extract the uploaded file and Apache HttpComponents Client to submit the uploaded file to the PHP script as if it's submitted from a HTML form.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
InputStream fileContent = null;
String fileContentType = null;
String fileName = null;
try {
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (!item.isFormField() && item.getFieldName().equals("file")) { // <input type="file" name="file">
fileContent = item.getInputStream();
fileContentType = item.getContentType();
fileName = FilenameUtils.getName(item.getName());
break; // If there are no other fields?
}
}
} catch (FileUploadException e) {
throw new ServletException("Parsing file upload failed.", e);
}
if (fileContent != null) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://example.com/upload.php");
MultipartEntity entity = new MultipartEntity();
entity.addPart("file", new InputStreamBody(fileContent, fileContentType, fileName));
httpPost.setEntity(entity);
HttpResponse phpResponse = httpClient.execute(httpPost);
// Do your thing with the PHP response.
}
}
See also: