开发者

Spring FileInputStream buffer sporadically contains incorrect characters

I am using FileInputStream in Spring MVC to read a chunk of a file into a byte[] buffer.

I then write (using FileCopyUtils.copy) the buffer to the response stream.

I notice that the response that is written to the stream (what the user receives) looks similar to the file, but there are blemishes.

  1. before the file data, there is a '2000' (without quotes) at the top of the file.
  2. this '2000' string is sporadically present throughout my file
  3. The file ends with a '0'

none of these exist in the original file that is being read from. Can anyone help me get rid of these so that I have an accurate output stream?

here's an example of what my incorrect ouput looks like:

2000
a line of data
a line of data
a line of data
a line of data
a line of data
a line of data
a line o
2000
f data
a line of data
2000

a line of data
a line of data
0

it should just be:

a line of data
a line of data
a line of data
a line of data
a line of da开发者_如何学Gota
a line of data
a line of data
a line of data
a line of data
a line of data

Thanks!


my guess is that the stream is specified as a chunked transfer encoding and your simply reading it as is


Nothing to do with FileInputStream. This is occurring during transmission to the client. Probably chunked encoding as suggested by ratchet freak. Use an HttpURLConnection at the client and itnwill handle that invisibly to you.


Can you try IOUtils.copy() from Apache Commons IO?

IOUtils.copy(new FileInputStream("some.file"), servletResponse.getOutputStream());

Hard to tell from your code, but most likely you have a bug somewhere while copying data from one place to another (index mismatch/missing flush, etc.) Use ready-made and well-tested stream copying routine to pinpoint the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜