cnestor
5 Nov 2009, 1:34 PM
I can successfully upload a file in hosted mode using the following code. When I compile and try in web mode, it works fine in IE6 but doesn't in google chrome. The form is submitted but it never reaches the POST method in my servlet.
It doesn't work for any browser when I deploy to google app engine.
I don't have any error messages in my log.
The form fires the Events.BeforeSubmit in any cases.
I am also using a GET method in the same servlet to monitor the progress from the client side using the ProgressListener class. This works also fine.
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, "/upload");
What am I doing wrong, it's driving me crazy?
Thanks a lot.
...
formPanel = new formPanel ();
formPanel.setHeaderVisible(false);
formPanel.setFrame(true);
formPanel.setAction("/upload");
formPanel.setEncoding(Encoding.MULTIPART);
formPanel.setMethod(Method.POST);
FileUploadField file = new FileUploadField();
...
Web.xml
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/trspring-config.xml /WEB-INF/trspring-Security-config.xml
</param-value>
</context-param>
<!-- Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Servlets -->
<servlet>
<servlet-name>DataSource</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DataSource</servlet-name>
<url-pattern>*.rpc</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>FileUploadServlet</servlet-name>
<servlet-class>com.tr.gxt.aoi.server.IO.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUploadServlet</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>AOIDashboard.html</welcome-file>
</welcome-file-list>
</web-app>
Servlet
public FileUploadServlet() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse resp)
throws ServletException {
logger.info("Server side - GET - FileUploadServlet");
HttpSession session = request.getSession();
logger.info("GET - Session: " +session.getId() + " - " +session.toString());
FileUploadListenerFix f = new FileUploadListenerFix();
FileUploadListener listener = (FileUploadListener) session.getAttribute("progress");
if (listener != null) {
StringBuilder msg = new StringBuilder();
msg.append(formatSize(listener.getBytesRead()));
msg.append("/");
msg.append(formatSize(listener.getContentLength()));
logger.info("message: " + msg);
try {
resp.getOutputStream().write(msg.toString().getBytes());
resp.getOutputStream().close();
} catch (IOException e) {
logger.error(e.getMessage());;
}
}
}
private String formatSize(long bytes) {
long kBytes = bytes / 1024;
if (kBytes > 1024) {
return kBytes / 1024 + "Mb";
} else {
return kBytes + "kb";
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException {
logger.info("Server side - POST - FileUploadServlet");
if(ServletFileUpload.isMultipartContent(request)) {
HttpSession session = request.getSession();
FileUploadListener listener = new FileUploadListener();
session.setAttribute("progress", listener);
ServletFileUpload upload = new ServletFileUpload();
upload.setProgressListener(listener);
fileDAO = new FileDAOImpl();
String userId = "Admin";
try {
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
if (item.isFormField()==true) {
if (item.getName()!=null&&item.getName().equals("userId")) {
userId = item.getFieldName();
}
}
if (item.isFormField()==false) {
String str = item.getName();
System.out.println("item: " + item.getName());
//Create new datastore object
TRFile file = new TRFile();
//Load filename
char[] chars = str.toCharArray();
int index = 0;
int index1 = 0;
for (; index < str.length(); index++) {
if (chars[index] == '\\')
index1 = index;
}
String fileName;
if (index1==0) {
fileName = str.substring(index1+1);
}else {
fileName = str.substring(index1+1);
}
file.setFileName(fileName);
//Load opportunityReport flag
if (item.getFieldName().indexOf("Opportunity") > -1) {
file.setOpportunityReport(true);
} else {
file.setOpportunityReport(false);
}
//Load file content
InputStream stream = item.openStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[8192];
while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
int maxFileSize = 1048576; //1 Mb max
if (out.size() > maxFileSize) {
System.out.println("File is > than "
+ maxFileSize);
return;
}
file.setFile(new Blob(out.toByteArray()));
file.setOwner(userId);
file.setContentType(item.getContentType());
//Add object to the datastore for Google App Engine(GAE) support using JDO framework.
fileDAO.add(file);
resp.getWriter().print(file.getId());
}
}
} catch (FileUploadException e1) {
System.out.println(e1.getMessage());
}
}
}
It doesn't work for any browser when I deploy to google app engine.
I don't have any error messages in my log.
The form fires the Events.BeforeSubmit in any cases.
I am also using a GET method in the same servlet to monitor the progress from the client side using the ProgressListener class. This works also fine.
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, "/upload");
What am I doing wrong, it's driving me crazy?
Thanks a lot.
...
formPanel = new formPanel ();
formPanel.setHeaderVisible(false);
formPanel.setFrame(true);
formPanel.setAction("/upload");
formPanel.setEncoding(Encoding.MULTIPART);
formPanel.setMethod(Method.POST);
FileUploadField file = new FileUploadField();
...
Web.xml
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/trspring-config.xml /WEB-INF/trspring-Security-config.xml
</param-value>
</context-param>
<!-- Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Servlets -->
<servlet>
<servlet-name>DataSource</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DataSource</servlet-name>
<url-pattern>*.rpc</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>FileUploadServlet</servlet-name>
<servlet-class>com.tr.gxt.aoi.server.IO.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUploadServlet</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>AOIDashboard.html</welcome-file>
</welcome-file-list>
</web-app>
Servlet
public FileUploadServlet() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse resp)
throws ServletException {
logger.info("Server side - GET - FileUploadServlet");
HttpSession session = request.getSession();
logger.info("GET - Session: " +session.getId() + " - " +session.toString());
FileUploadListenerFix f = new FileUploadListenerFix();
FileUploadListener listener = (FileUploadListener) session.getAttribute("progress");
if (listener != null) {
StringBuilder msg = new StringBuilder();
msg.append(formatSize(listener.getBytesRead()));
msg.append("/");
msg.append(formatSize(listener.getContentLength()));
logger.info("message: " + msg);
try {
resp.getOutputStream().write(msg.toString().getBytes());
resp.getOutputStream().close();
} catch (IOException e) {
logger.error(e.getMessage());;
}
}
}
private String formatSize(long bytes) {
long kBytes = bytes / 1024;
if (kBytes > 1024) {
return kBytes / 1024 + "Mb";
} else {
return kBytes + "kb";
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException {
logger.info("Server side - POST - FileUploadServlet");
if(ServletFileUpload.isMultipartContent(request)) {
HttpSession session = request.getSession();
FileUploadListener listener = new FileUploadListener();
session.setAttribute("progress", listener);
ServletFileUpload upload = new ServletFileUpload();
upload.setProgressListener(listener);
fileDAO = new FileDAOImpl();
String userId = "Admin";
try {
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
if (item.isFormField()==true) {
if (item.getName()!=null&&item.getName().equals("userId")) {
userId = item.getFieldName();
}
}
if (item.isFormField()==false) {
String str = item.getName();
System.out.println("item: " + item.getName());
//Create new datastore object
TRFile file = new TRFile();
//Load filename
char[] chars = str.toCharArray();
int index = 0;
int index1 = 0;
for (; index < str.length(); index++) {
if (chars[index] == '\\')
index1 = index;
}
String fileName;
if (index1==0) {
fileName = str.substring(index1+1);
}else {
fileName = str.substring(index1+1);
}
file.setFileName(fileName);
//Load opportunityReport flag
if (item.getFieldName().indexOf("Opportunity") > -1) {
file.setOpportunityReport(true);
} else {
file.setOpportunityReport(false);
}
//Load file content
InputStream stream = item.openStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[8192];
while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
int maxFileSize = 1048576; //1 Mb max
if (out.size() > maxFileSize) {
System.out.println("File is > than "
+ maxFileSize);
return;
}
file.setFile(new Blob(out.toByteArray()));
file.setOwner(userId);
file.setContentType(item.getContentType());
//Add object to the datastore for Google App Engine(GAE) support using JDO framework.
fileDAO.add(file);
resp.getWriter().print(file.getId());
}
}
} catch (FileUploadException e1) {
System.out.println(e1.getMessage());
}
}
}