cbandroid
cbandroid

Reputation: 21

Uploading files to a server using java

I am writing a simple java servlet to upload files to a server location from any client. This is my servlet doPost method:

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        response.setContentType("text/html");
        boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
        if(isMultiPart){
            ServletFileUpload upload = new ServletFileUpload();
            try{
                FileItemIterator itr = upload.getItemIterator(request);
                while(itr.hasNext()){
                    FileItemStream item = itr.next();
                    if(item.isFormField()){
                        String fieldName = item.getFieldName();
                        InputStream is = item.openStream();
                        byte[] b = new byte[is.available()];
                        is.read(b);
                        String value = new String(b);
                        response.getWriter().println(fieldName+":"+value+"</br>");

                    }else{
                        //String path = getServletContext().getRealPath("/");
                        String path = <server path>;
                        if(FileUpload.processFile(path, item) ){
                            response.getWriter().println("file uploaded successfully</br>");
                        }else{
                            response.getWriter().println("file uploading failed</br>");
                        }
                    }
                }
            }catch(FileUploadException fue){
                fue.printStackTrace();
            }
        }
    }

I now have a problem when I am uploading very big files. Incase there is a network error, I need to resend all the files to the server again, I would like to prepare a solution where the user can upload the files from the point where it stopped when the network error occurred. Can someone help me with this? Thanks in advance.

Can anyone help me with this please?

Upvotes: 0

Views: 1914

Answers (1)

Related Questions