This question already has answers here:
How can I upload files to a server using JSP/Servlet?
(14 answers)
Closed 3 years ago.
i'm writing a servlet that receives a xml file, gives it to another class and gives a html file with some comments back to the client.
I'm getting the input-xml with something like:
input = request.getInputStream();
but this input is a ServletInputStream and the other class(for the comments) needs a FileInputStream.
If i give the XMLEventReader(in the other class) the ServletInputStream, i get a parsing error:
ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
And i think this is because of the servletinputstream instead of the fileinputstream.
Greetings
i hope somebody is able to help me:)
I think it's unlikely that the latter problem is due to it being a ServletInputStream. The parser shouldn't care about the source of the data.
I think it's rather more likely that the data in the input stream is incorrect.
Your class which currently requires FileInputStream should be refactored to work with InputStream if at all possible... otherwise you'll have to write the data to an actual file first, which is obviously not ideal.
you should read the data from the ServletInputStream, and dump it into a FileOutputStream. this way you can look at the data that is being sent, then test that data separately using FileInputStream with the class you described that needs it. my guess is that the same thing will happen as is happening now since like the previous poster suggested, the data is probably in the wrong format.
Okay, i am now a bit smarter:)
The problem is: In the ServletInputStream is at the beginning(and one line at the end) some header information (as content-type, etc..).
Is there a smart solution for cutting this information?
greetings
Related
I have searched web and StackOverflow but did not find anything that would answer this specifically, I found answers like one cannot use RandomAccessFile() for reading a folder - OK, but is there something similar?
I have a piece of a code that originally read from a special type of a file using RandomAcessFile(), but as I have changed a lot of things in the content of that file by extracting them to a folder but there is no way how to pack them back to the original format (it can be extracted with a technique but there is no technique for packing it back) I need to now read it as a directory instead of a file. My problem is that there is a huge amount of classes made around it using RandomAccessFile() output so changing it all just because of this would be a "no-go".
Therefore my question is: what is - if any at all - some kind of equivalent that would output/return something like RandomAccessFile() output but for a directry/folder?
Specifically I would only need to update/change/fix that file variable in this part of the code:
RandomAccessFile file = new RandomAccessFile(lifFile, "r");
long positionOffset = 0;
LIFFile rootFile = parseLIFFile(file, positionOffset);
LIFReader reader = new LIFReader(file, lifFile, rootFile, positionOffset);
return reader;
Can anyone tell/help how to do something like that?
EDIT:
Just a clarification: that special "original" file actually hold quantum of other files like images, 3D geometries, xml files in separate directories...
EDIT 2: So I have already solved it by creating completely new constructor (derivated form the original code) as suggested below - thanx for your thoughts, guys.
This question already has answers here:
Inserting text into an existing file via Java
(8 answers)
Closed 6 years ago.
In my project, we are writing a file using DataOutputStream. We are writing different data types like short, byte, int and long and we are using respective methods in DataOutputStream like writeShort(), writeByte() etc.
Now, I want to edit one record in this file at a particular offset. I know the offset from which that record starts but I am not sure what is the right approach of writing to the file because only method in DataOutputStream supporting offset is the one which takes byte[].
I want to write the whole record which is a combination of different data types as mentioned above.
Can someone please tell me what is the correct approach for this?
In your case, you should use RandomAccessFile in order to read and/or write some content in a file at a given location thanks to its method seek(long pos).
For example:
try (RandomAccessFile raf = new RandomAccessFile(filePath, "rw")) {
raf.seek(offset);
// do something here
}
NB: The methods writeShort(), writeByte() etc. and their read counterparts are directly available from the class RandomAccessFile so using it alone is enough.
This question already has answers here:
What's the difference between InputStream and ByteArrayInputStream?
(4 answers)
Closed 6 years ago.
What is the difference between FileInputStream and ByteArrayInputStream?
When we should use them?
What is the benefits of using one over other
Streams
byte oriented stream (8 bit)
good for binary data such as a Java .class file, images etc.
good for "machine-oriented"
Readers/Writers
char (utf-16) oriented stream (16 bit)
one character at a time
good for text such as a Java source
good for "human-oriented" data
Buffered
many bytes/characters at a time
always useful unless proven otherwise
Both ByteArrayInputStream and FileInputStream serve the same purpose i.e. reading binary data and both implement a common abstract superclass InputStream.So it is really tough find out a sharp contrast among the two.But common logic dictates that arrays are already present in the memory hence they can be accessed much faster as compared to files which are present in the file system. Also if you go through the documentation of the two classes you will find out that read function of ByteArrayInputStream cannot block whereas the read function in FileInputStream blocks if no input is yet available.
So if you are confused about what to use when then think about these :
In what form my data is present, file or array?
Is speed really a requirement? If it is, then go for ByteArray. But remember you cannot store too much information in ByteArray as they are present in the memory.
This question already has answers here:
How to get a file's Media Type (MIME type)?
(28 answers)
Closed 7 years ago.
Is it possible to get content type/mime type of a file from the file read as a stream from its stored location.
I am bit new to the file and stuffs, I was searching all day long but couldn't get what i have needed.
please help me out of this. thanks in advance
Edit:-
Edit:
Guys thanks for your comments.
My java version is 6 and i can't trust the filename to get its mime type.
Use this:
Files.probeContentType(path)
By the way your question is a duplicate of this SO post. Please check SO thoroughly before asking your next question.
A MIME type of a file is not part of the file itself. Rather, it is typically configured and provided by the server from which the file is accessed; through an HTTP header for example. Some binary files have what are called "magic numbers" which can, to some degree, be used to identify the contents of the file. See here for a catalog of common ones. For example, you may be able to look at the first few bytes of the file stream and check the catalog to determine the type. However, there is no guarantee that this method will be accurate.
As others have suggested, you can use Files#probeContentType(), but that takes a file name. If you only have access to a stream, your out-of-luck. Also, it too suffers from the same accuracy issues.
From Java Docs : http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#probeContentType(java.nio.file.Path)
Use
probeContentType()
Maybe this link will help:
mime type of files
In Java, how would I convert a byte array (TCP packet payload from a pcap file) into some kind of HTTP object that I can use to get HTTP headers and content body?
One of the stupid lovely things about Java is a total lack of unsigned types. So, a good place to start would be taking your byte array and converting it into a short array to make sure that you don't have any rollover problems. (16 bits versus 8 bits per number).
From there, you could use a BufferedOutputStream to write your data to a file and parse it with one of the Java built-in XML readers, such as JaxB or DOM. BufferedOutputStream writes hex directly to a file, and can take an input of an int, byte, or short array. After you write it out, using the OutputStream it should be very simple to parse the HTML out of it.
If you need any help with any of these individual steps, I'd be happy to help.
EDIT: as maerics has pointed out, perhaps I didn't grasp what you were asking. Regardless, writing your byte array with a BufferedOutputStream is the way to go in my opinion, and I could still help you build a parser if you want.
JNetPcap can do exactly this.
Here are examples for
Opening a pcap file
Parsing http (in the example, we extract an image)
Drawback: parsing http in this library is depracated*, but that doesn't mean it doesn't work
*I can't post anymore links without more reputation. Sorry. You can Google for "jnetpcap http deprecated"