I get 404 error. Simply the file is not written in HDD. Like I said,
when i run the app in hosted mode, this works.
Here is the servlet code:
package com.myapplication.server;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
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.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.myapplication.client.MyApplication;
public class FileUploadServlet extends HttpServlet {
private static final long MAX_SIZE = 1024 * 1024 * 1024 * 4;
private static final String CONTENT_TYPE_UNACCEPTABLE = "{error:
'File upload failed. "
+ " EXE file can't be uploaded'}" + MAX_SIZE + " bytes or
less'}";
private static final String KEY_WORD = "FILENAME";
private static final String DATA = "FILEDATA";
@Override
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
String json = null;
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
} catch (Exception e) {
e.printStackTrace();
}
Iterator it = items.iterator();
String tmp = "";
FileItem data = null;
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (item.getFieldName().equalsIgnoreCase(KEY_WORD)) {
tmp = new String(item.get());
} else if (item.getFieldName().equalsIgnoreCase(DATA)) {
data = item;
}
}
json = processFile(data, tmp);
response.setContentType("text/plain");
response.getWriter().write(json);
}
/**
*
* @param item
* @return
*/
private String processFile(FileItem item, String fileName) {
if (!isContentTypeAcceptable(item)) {
return CONTENT_TYPE_UNACCEPTABLE;
}
try {
File fl = new File("/tmp/" + fileName + MyServiceImpl.getLogin
());
fl.deleteOnExit();
fl.createNewFile();
BufferedInputStream b = new BufferedInputStream(item
.getInputStream());
int i = -1;
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(fl));
while ((i = b.read()) != -1) {
out.write((char) i);
}
out.close();
b.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "good";
}
/**
*
* @param item
* @return
*/
private boolean isSizeAcceptable(FileItem item) {
return item.getSize() <= MAX_SIZE;
}
/**
*
* @param item
* @return
*/
private boolean isContentTypeAcceptable(FileItem item) {
return true;// !item.getContentType().equalsIgnoreCase
(ACCEPTABLE_CONTENT_TYPE);
}
}
On 11 kvě, 17:03, gwtGrady <[email protected]> wrote:
> Whe you say "uploading file does not work", what exactly do you mean?
> Any error message?
>
> On May 10, 8:08 am, tom_gwt <[email protected]> wrote:
>
> > Hi everyone,
> > i'm using GWT 1.5.3 and gwtswfext (handling file upload on client
> > side, javascript/flash library
> > -http://code.google.com/p/gwtswfext/wiki/GWTSWFUpload).
> > When I run application in hosted mode, uploading file works fine -
> > file is saved in e.g. /tmp/ So I compile the application and run it in
> > tomcat server and uploading file does not work. I tried add file
> > permission in tomcat configuration file (04webapps.policy) but file
> > upload still doesnt work. The problem is obviously on the server side,
> > file upload works in hosted mode and doesnt work when the app is
> > running at tomcat server.
>
> > 04webapps.policy:
>
> > grant codeBase "file:${catalina.home}/webapps/-"
> > {
> > permission java.io.FilePermission "file:/tmp/-", "read, write,
> > delete";
>
> > };
>
> > Please help me..
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---