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();
              int i;
              InputStream input;
              input = request.getInputStream();
              BufferedInputStream in =
                 new BufferedInputStream(input);
              BufferedReader reader =
                 new BufferedReader(
                   new InputStreamReader(in));
              File outputFile= new File(this.getServletContext().getRealPath("/"), "sting.mp3");
              FileWriter out =
                 new FileWriter(outputFile);
              while ((i = != -1)
            catch (IOException e) {

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"));
            } catch (FileNotFoundException e) {
            } catch (HttpException e) {
            } catch (IOException e) {


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

please help:

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