View Full Version : Can't get status of file upload to be returned

8 Aug 2011, 6:00 AM

I have gotten the file upload to work with extjs and a servlet, but cannot let the user know the results of the upload. The status is not seen by the extjs, although it is set by the servlet.

I have read in other posts on this forum that if the servlet sets the content type to text/html, and sets the status in the body of the returned html, that extjs will parse the results and extract the status. I don't see this.

Here is my servlet code:

package test;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

* Servlet implementation class MyFileUploadServlet
public class MyFileUploadServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

private static final String TMP_DIR_PATH = "c:\\tmp";
private File tmpDir;
private static final String DESTINATION_DIR_PATH ="/files";
private File destinationDir;

* Default constructor.
public MyFileUploadServlet() {
// TODO Auto-generated constructor stub

public void init(ServletConfig config) throws ServletException {
tmpDir = new File(TMP_DIR_PATH);
if(!tmpDir.isDirectory()) {
throw new ServletException(TMP_DIR_PATH + " is not a directory");
//String realPath = getServletContext().getRealPath(DESTINATION_DIR_PATH);
destinationDir = new File("c:\\files");
if(!destinationDir.isDirectory()) {
throw new ServletException(DESTINATION_DIR_PATH+" is not a directory");

* @see HttpServlet#HttpServlet()
public MyFileUploadServlet() {
// TODO Auto-generated constructor stub

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String contentHeader = request.getHeader("content-type");
boolean isMultipart = (contentHeader != null && contentHeader.indexOf("multipart/form-data") != -1);

if (isMultipart == false)
System.out.println("multipart not found");
System.out.println("multipart found");

PrintWriter out = response.getWriter();

DiskFileItemFactory fileItemFactory = new DiskFileItemFactory ();

fileItemFactory.setSizeThreshold(1*1024*1024); //1 MB


ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);

try {

List items = uploadHandler.parseRequest(request);

Iterator itr = items.iterator();
while(itr.hasNext()) {
FileItem item = (FileItem) itr.next();

if(item.isFormField()) {
} else {
File file = new File(destinationDir,item.getName());

out.println("<html><h1>Servlet File Upload Example using Commons File Upload</h1><body>{success: true}</body></html>");


}catch(FileUploadException ex) {
log("Error encountered while parsing the request",ex);
out.println("<html><h1>Servlet File Upload Example using Commons File Upload</h1><body>{'success': false}</body></html>");

catch(Exception ex) {
out.println("<html><h1>Servlet File Upload Example using Commons File Upload</h1><body>{'success': false}</body></html>");

System.out.println("An error has occurred: " + ex);
log("Error encountered while uploading file",ex);



Here is the extjs code:

File Upload


Ext.create('Ext.form.Panel', {
//fileUpload: true,
url: 'MyFileUploadServlet',
renderTo: 'fi-form',
width: 500,
frame: true,
title: 'File Upload Form',
bodyPadding: '10 10 0',

defaults: {
anchor: '100%',
allowBlank: false,
msgTarget: 'side',
labelWidth: 50
items: [{
xtype: 'filefield',
id: 'form-file',
emptyText: 'Select a file',
fieldLabel: 'File',
name: 'file-path',

buttons: [{
text: 'Upload',
handler: function(){
var form = this.up('form').getForm();
success: function(f,a){
failure: function(f,a){
Ext.Msg.alert('not worked');

Neither one of the alerts come up after the file is successfully uploaded.

Thanks so much for any help.

8 Aug 2011, 6:41 AM
The html returned from the servlet was incorrect. The H1 tag was not inside the body tag.