File Uploading


General J2EE: File Uploading

  1. File Uploading (3 messages)

    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,

    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 {
                                out = res.getWriter();
                                String contentType = req.getContentType();
                                java.util.Enumeration enum =
                                while(enum.hasMoreElements()) {
                                out.println(" param = " +
                                if ((contentType != null)
                                && (contentType.indexOf("multipart/form-
                                data") >= 0)) {
                                in = new DataInputStream
                                int formDataLength = req.getContentLength

                                if (formDataLength > MAX_SIZE) {
                                out.println("Sorry file is too
                                large ");

                                byte dataBytes[] = new byte
                                int byteRead = 0;
                                int totalBytesRead = 0;
                                while (totalBytesRead < formDataLength) {
                                byteRead =,
                                totalBytesRead, formDataLength);
                                totalBytesRead += byteRead;


                                String file = new String(dataBytes);
                                String saveFile = file.substring
                                (file.indexOf("filename=\"") + 10);
                                saveFile = saveFile.substring(0,
                                saveFile =
    ") + 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,
  4. File Uploading[ Go to top ]

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