I'm new to GWT app and I need help with file download
My file upload is so
clint
//-------------------upload--------------------------

final FormPanel form = new FormPanel();
form.setAction(UPLOAD_ACTION_URL);

// Because we're going to add a FileUpload widget, we'll need to set
the
// form to use the POST method, and multipart MIME encoding.
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);

// Create a TextBox, giving it a name so that it will be submitted.
final TextBox tb = new TextBox();
tb.setName("textBoxFormElement");
panel.add(tb);

// Create a FileUpload widget.
final FileUpload upload = new FileUpload();
upload.setName("uploadFormElement");
panel.add(upload);

// Add a 'submit' button.
panel.add(new Button("Submit", new ClickHandler() {
        public void onClick(ClickEvent event) {
                form.submit();
        }
}));

// Add an event handler to the form.
form.addSubmitHandler(new FormPanel.SubmitHandler() {
        public void onSubmit(SubmitEvent event) {
                // This event is fired just before the form is submitted. We can
                // take this opportunity to perform validation.

                //if (tb.getText().length() == 0) {
                        //Window.alert("The text box must not be empty");
                        //event.cancel();
                //}


        }
});

form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
        public void onSubmitComplete(SubmitCompleteEvent event) {
                // When the form submission is successfully completed, this
                // event is fired. Assuming the service returned a response of 
type
                // text/html, we can get the result text here (see the FormPanel
                // documentation for further explanation).
                Window.alert(event.getResults());
                FileName=upload.getFilename();
                tb.setText(FileName);
        }
});




RootPanel.get().add(form);

Servlet:

package de.tuberlin.pjel2009.server;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
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 org.apache.commons.io.FilenameUtils;

/**
 * servlet to handle file upload requests
 *
 *
 *
 */
public class FileUploadServlet extends HttpServlet {

        //private static final String UPLOAD_DIRECTORY = "d:\\uploaded\\";


        @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
                final String UPLOAD_DIRECTORY=req.getSession().getServletContext
().getRealPath("/")+ "job\\";
                if (ServletFileUpload.isMultipartContent(req)) {

                        File tempDir = getTempDir(UPLOAD_DIRECTORY);
                        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 = new 
File(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(String Dir) {
                return new File(Dir);
        }


 we upload the file in a folder called job
on hyperlink or button --> download file from this folder

Can you give me an example to:

1) client-side
2) server-side
3) web.xml

Thanks!!

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to google-web-tool...@googlegroups.com.
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to