Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8166363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:50:19+00:00 2026-06-06T19:50:19+00:00

I am trying to read a wav file from the resources of my Java

  • 0

I am trying to read a wav file from the resources of my Java Spring project and then convert the file to a byte array and write it to outputstream of HttpResponse. When I receive the file from my web application it is broken with garbage data.

Here is the code in my servlet:

response.setContentType("audio/wav");
response.setHeader("Content-Disposition", "attachment;filename=audio-qrcode-"+"hellosine"+".wav");
response.setHeader("Pragma", "private");
response.setHeader("Cache-Control", "private, must-revalidate");
response.setHeader("Accept-Ranges", "bytes")

URL helloSineWav = QRcodeController.class.getClassLoader().getResource("hellosine.wav");
byte[] audioBytes = IOUtils.toByteArray(helloSineWav.openStream());
response.setContentLength(audioBytes.length);
ServletOutputStream outStream = response.getOutputStream();
outStream.write( audioBytes );
outStream.flush();
outStream.close();

Another way I tried to approach the solution was:

ServletOutputStream out = response.getOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
    out.write(buf, 0, len);
}
is.close();
out.close();

Both approach work but give me a corrupted audio file at the other end. I am not exactly sure why this is happening.
Is it something to do with the header?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:50:22+00:00Added an answer on June 6, 2026 at 7:50 pm

    I figured out the problem. It wasn’t really related to httpResponse or any of this. I added the wave file to the Java Project and packaged a war file from it. But when I deploy the project and the resource file gets unpacked it changes the size of the file from the original and introduces some garbage information. I am currently looking into why that happens. But as far as this question is concerned, when I replaced the wave file with the correct size one it works. Sorry for bothering you guys.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read a .wav file into an array of bytes to be
I am trying to read bytes from a wav file and send it across
I am trying to read data of a .wav file both in java and
I am trying to load a *.wav file to a byte array using C#
Im trying to read and write to/from the same file, is this possible? Here
I am trying to read a single file from a java.util.zip.ZipInputStream , and copy
Naudio Library: http://naudio.codeplex.com/ I'm trying to convert an MP3 file to a WAV file,
am trying to read web page from java code and its fine for general
When trying to read tokens from a file in C++, I receive a seg
I'm trying to read a WAV file (and in the future also MP3 and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.