Discussions

General J2EE: File Uploading

  1. File Uploading (3 messages)

    Hello,
    We are in the process of developing a J2EE application. In this, we require to upload a file from client end to server end. What are the posible ways to do this? Is Applet the only way?

    Thanks and Regards,
    Pankaj.

    Threaded Messages (3)

  2. File Uploading[ Go to top ]

    You can do it with a Servlet.
    Here's some sample code.

    public void doPost(HttpServletRequest req, HttpServletResponse
                                res) {
                                PrintWriter out = null;
                                DataInputStream in = null;
                                FileOutputStream fileOut = null;
                                rootPath = "c:\\upload
    ";
                                try {
                                res.setContentType("text/html");
                                out = res.getWriter();
                                String contentType = req.getContentType();
                                java.util.Enumeration enum =
                                req.getParameterNames();
                                while(enum.hasMoreElements()) {
                                out.println(" param = " +
                                (String)enum.nextElement());
                                }
                                out.println(contentType);
                                if ((contentType != null)
                                && (contentType.indexOf("multipart/form-
                                data") >= 0)) {
                                in = new DataInputStream
                                (req.getInputStream());
                                int formDataLength = req.getContentLength
                                ();

                                out.println("1<br>");
                                if (formDataLength > MAX_SIZE) {
                                out.println("Sorry file is too
                                large ");
                                out.flush();
                                return;

                                }
                                out.println("2<br>");
                                byte dataBytes[] = new byte
                                [formDataLength];
                                int byteRead = 0;
                                int totalBytesRead = 0;
                                while (totalBytesRead < formDataLength) {
                                byteRead = in.read(dataBytes,
                                totalBytesRead, formDataLength);
                                totalBytesRead += byteRead;

                                }
                                out.println("3<br>");

                                String file = new String(dataBytes);
                                String saveFile = file.substring
                                (file.indexOf("filename=\"") + 10);
                                saveFile = saveFile.substring(0,
                                saveFile.indexOf("\n"));
                                saveFile =
                                saveFile.substring
                                (saveFile.lastIndexOf("
    ") + 1, saveFile.indexOf("\""));
                                int lastIndex = contentType.lastIndexOf
                                ("=");
                                String boundary = contentType.substring
                                (lastIndex + 1, contentType.length());


    The rest of the code got cutoff because TSS had a message length restriction.
  3. File Uploading[ Go to top ]

    Hello Sheng,
    I couldn't get complete code. Can you please send it back to pankaj08 at sify dot com. I will be thankfull to you.

    Thanks and Regards,
    Pankaj.
  4. File Uploading[ Go to top ]

    the propably best way to do this is using jason hunters MultipartRequest, see www.servlets.com for details and code downloads.


    Regards,
    Kari