1

I am trying to avoid the FileItem getInputStream(), because it will get the wrong encoding, for that I need a FileInputStream instead. Is there any way to get a FileInputStream without using this method? Or can I transform my fileitem into a file?

if (this.strEncoding != null && !this.strEncoding.isEmpty()) {
    br = new BufferedReader(new InputStreamReader(clsFile.getInputStream(), this.strEncoding));
} 
else {
    // br = ?????
}
hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
  • It would be great if you can share sample code. – Braj Jul 22 '14 at 16:49
  • Are you talking about FileItem from Apache commons? – Swapnil Jul 22 '14 at 16:50
  • 2
    An `InputStream` has no encoding in Java; it deals with `byte`s. Going from an `InputStream` to a `Reader` generally involves defining an encoding, but that's not a Stream issue. If the way you obtain your `InputStream` infers with the encoding you get, then I suspect something undefined / strange / default / platform specific is at work, which may later turn into a bug. – GPI Jul 22 '14 at 16:54
  • What is the file type? Is it plain text file or binary file? – Braj Jul 22 '14 at 16:58
  • As @GPI said, you have a misunderstanding, as an `InputStream` does not have an encoding. Show us the code you are using that is reading from `FileItem.getInputStream()` and we can tell you what you're doing wrong. – Christoffer Hammarström Jul 22 '14 at 17:16
  • example: if ( this.strEncoding != null && !this.strEncoding.isEmpty () ) { br = new BufferedReader ( new InputStreamReader ( clsFile.getInputStream (), this.strEncoding ) ); } else { ????????????? } – Luisa Mesquita Jul 23 '14 at 14:24

3 Answers3

2

You can try

FileItem#getString(encoding)

Returns the contents of the file item as a String, using the specified encoding.

Braj
  • 46,415
  • 5
  • 60
  • 76
0

You can use the write method here.

File file = new File("/path/to/file");
fileItem.write(file);
Swapnil
  • 8,201
  • 4
  • 38
  • 57
0

An InputStream is binary data, bytes. It must be converted to text by giving the encoding of those bytes.

Java uses internally Unicode to represent all text scripts. For text it uses String/char/Reader/Writer.

For binary data, byte[], InputStream, OutputStream.

So you could use a bridging class, like InputStreamReader:

String encoding = "UTF-8"; // Or "Windows-1252" ...
BufferedReader in = new BufferedStream(
    new InputStreamReader(fileItem.getInputStream(),
                          encoding));

Or if you read the bytes:

String s = new String(bytes, encoding);

The encoding is often an option parameter (there then exists an overloaded method without encoding).

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • There is a simple method `FileItem#getString()` if you are talking about text file. what about binary files. Can I use it for images? – Braj Jul 22 '14 at 17:21
  • You are right on getString (+1), though I felt called to pray about the schism between Binary and Text; as that _is_ what seems to be unclear, and likely erroneously used. Binary files should never be stored in String. – Joop Eggen Jul 22 '14 at 17:27
  • As per my knowledge `Reader` works for text stream only. – Braj Jul 22 '14 at 17:28
  • @Braj yes, you can use a ~Reader only to read text, and that seems to be the problem: there is resulting text in the wrong encoding. So a conversion of the binary file did not specify an encoding. – Joop Eggen Jul 22 '14 at 17:33
  • I dont have the encoding. can be anything – Luisa Mesquita Jul 23 '14 at 14:21
  • @Braj the getString will use the default encoding, and i dont know the encoding, can be anything – Luisa Mesquita Jul 23 '14 at 14:22
  • So you'll have to try out encodings. A dump of the byte values might help. Or some info on the language the text is in. Sorry. – Joop Eggen Jul 23 '14 at 15:10