troubleshooting Question

How to upload mp3 to servlet

Avatar of dcave555
dcave555 asked on
Java EE
2 Comments1 Solution850 ViewsLast Modified:
Hi i need to upload mp3 to servlet in doPut

My code is:
      public void doPut(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {

            System.out.println("I am here");
            System.out.println("Content Type ="+request.getContentType());
            
             PrintWriter outHTML = response.getWriter();
           outHTML.println("done");
              
           try
           {
              int i;
              InputStream input;
              input = request.getInputStream();
              BufferedInputStream in =
                 new BufferedInputStream(input);
              BufferedReader reader =
                 new BufferedReader(
                   new InputStreamReader(in));
              System.out.println(this.getServletContext().getRealPath("/"));
              File outputFile= new File(this.getServletContext().getRealPath("/"), "sting.mp3");
              FileWriter out =
                 new FileWriter(outputFile);
          
              while ((i = reader.read()) != -1)
              {
                out.write(i);
              }
          
              out.close();
              in.close();
          
            }
            catch (IOException e) {
                  e.printStackTrace();
            }
            
      }


and  my client is httpClient:
public static void main(String[] args) {
            HttpClient client = new HttpClient();
            String url = "http://localhost:8080/MyFileUploader/servlet/TestFileUpload";
            PutMethod put = new PutMethod(url);
            try {
                  put.setRequestHeader("Content-type", "audio/mpeg");
                  put.setRequestBody(new FileInputStream("resources/Fragile.mp3"));
                  client.executeMethod(put);
            } catch (FileNotFoundException e) {
                  e.printStackTrace();
            } catch (HttpException e) {
                  e.printStackTrace();
            } catch (IOException e) {
                  e.printStackTrace();
            }

      }


i can upload file but when i trying to play uploaded file its corupted

please help:
Thx
ASKER CERTIFIED SOLUTION
Kuldeepchaturvedi

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros