Monday, April 21, 2014

Re: browse a file from my desktop

A problem I see (and there may be more) is your line:

File uploadedFile = File.createTempFile(UPLOAD_DIRECTORY,fileName);

Look at the javadoc for that call:  http://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String, java.lang.String)

You are sending a prefix of "C:\\Users\\dell\\Downloads\\" and a suffix something like MyFile.txt. Those are neither a suitable prefix nor a suitable suffix. Java inserts a random string between the prefix and suffix and tries to create that file in java.io.tmpdir. Your call is telling java to create a filename--not a path, but a filename--similar to "C:\\Users\\dell\\Downloads\\1234567890MyFile.txt".  That is not a file name and the call to create the file fails.

Try instead something like 

File uploadedFile = File.createTempFile("imen", ".tmp", UPLOAD_DIRECTORY);

That should create a file similar to imen1234567890.tmp in C:\\Users\\dell\\Downloads\\

I think we are out the GWT area and into the Apache Commons area (http://commons.apache.org/proper/commons-fileupload/index.html). I suggest if you have further problems exclude GWT by trying file upload from a simple HTML form. If you're servlet still won't work, maybe some on the Apache Commons list can help.


On Monday, April 21, 2014 5:48:45 PM UTC-4, imen boukhris wrote:
yes thad i am using a real directory on my computer.
this is my entry point
public class UploadFile implements EntryPoint {
        private static final String UPLOAD_ACTION_URL = GWT.getModuleBaseURL()
                        + "upload";
        
       
        private static final String SERVER_ERROR = "An error occurred while "
                        + "attempting to contact the server. Please check your network "
                        + "connection and try again.";

        public void onModuleLoad() {
                // Create a FormPanel and point it at a service.
                final FormPanel form = new FormPanel();
                form.setAction(UPLOAD_ACTION_URL);
                form.setEncoding(FormPanel.ENCODING_MULTIPART);
                form.setMethod(FormPanel.METHOD_POST);

                // Create a panel to hold all of the form widgets.
                VerticalPanel panel = new VerticalPanel();
                form.setWidget(panel);
                ListBox lb = new ListBox();
                FileUpload upload = new FileUpload();
                upload.setName("uploadFormElement");
                panel.add(upload);
                panel.add(new Button("Submit", new ClickHandler() {
                        public void onClick(ClickEvent event) {
                       
                                form.submit();
                        }
                }));

                form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
                        public void onSubmitComplete(SubmitCompleteEvent event) {
                             
                                Window.alert(event.getResults());
                        }
                });

                RootPanel.get().add(form);

        }

}
this is my servlet
public class FileUploadServlet extends HttpServlet {

private static final long serialVersionUID = 1L;
private static final String UPLOAD_DIRECTORY = "C:\\Users\\dell\\Downloads\\";
        private static final String DEFAULT_TEMP_DIR = "upload";

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                        throws ServletException, IOException {
                // TODO Auto-generated method stub
                super.doGet(req, resp);
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                        throws ServletException, IOException {
                
                // process only multipart requests
                if (ServletFileUpload.isMultipartContent(req)) {

                       File tempDir = getTempDir();
                        if (!tempDir.exists()) {
                                tempDir.mkdirs();
                        }
                        // Create a factory for disk-based file items
                        FileItemFactory factory = new DiskFileItemFactory();
                        // Create a new file upload handler
                        ServletFileUpload upload = new ServletFileUpload(factory);
                        // Parse the request
                        try {
                                List<FileItem> items = upload.parseRequest(req);
                                for (FileItem fileItem : items) {
                                        // process only file upload
                                        if (fileItem.isFormField()) continue;
                                        
                                        String fileName = fileItem.getName();
                                        // get only the file name not whole path
                                        if (fileName != null) {
                                        fileName = FilenameUtils. getName(fileName);
                                    }

                                        File uploadedFile = File.createTempFile(UPLOAD_DIRECTORY,fileName);
                                   
                                        if (uploadedFile.createNewFile()) {
                                                fileItem.write(uploadedFile);
                                                resp.setStatus(HttpServletResponse.SC_CREATED);
                                                resp.getWriter().print("The file was created successfully.");
                                                resp.flushBuffer();
                                        } else
                                                throw new IOException("The file already exists in repository.");
                                }
                        } catch (Exception e) {
                                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                                "An error occurred while creating the file : " + e.getMessage());
                        }

                } else {
                        resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,
                                                        "Request contents type is not supported by the servlet.");
                }
        }

    private File getTempDir() {

                return new File(DEFAULT_TEMP_DIR);
        }

an finally my web.xml
<servlet>
    <servlet-name>uploadServlet</servlet-name>
    <servlet-class>com.gwt.upload.server.FileUploadServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>uploadServlet</servlet-name>
    <url-pattern>/uploadfile/upload</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>UploadFile.html</welcome-file>
  </welcome-file-list>

</web-app>

and now i am getting this error 
Le jeudi 3 avril 2014 00:36:28 UTC+2, imen boukhris a écrit :
Hi GWT community,
how can i upload a file to server from my desktop in my google web toolkit project.Anyone could provide me with good tutorial that explain the file upload solution from start  to end please.
thanks in advance
 

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment