outputstream

Sending file through ObjectOutputStream and then saving it in Java?

霸气de小男生 提交于 2019-12-01 04:56:48
问题 I have this simple Server/Client application. I'm trying to get the Server to send a file through an OutputStream (FileOutputStream, OutputStream, ObjectOutputStream, etc) and receive it at the client side before saving it into an actual file. The problem is, I've tried doing this but it keeps failing. Whenever I create the file and write the object I received from the server into it, I get a broken image (I just save it as a jpg, but that shouldn't matter). Here are the parts of the code

Java file download hangs

為{幸葍}努か 提交于 2019-12-01 04:12:50
问题 I have a web interface which is used to download a file. When the request comes in, my glassfish server streams the file from a web service and then writes the content to the outputstream. My code works fine except when the file size becomes very large (like more than 200 MB), it hangs showing 0% donwloaded in the browser and the file is never downloaded. When I move flush() method inside the while loop it works fine for large files as well. I am not sure if putting flush() in a loop is a

How to add Attachments to Email in java using outputstream

非 Y 不嫁゛ 提交于 2019-12-01 03:23:36
问题 I've seen the code for javax.mail library where you add attachments to the email doing this: MimeBodyPart attachmentPart = new MimeBodyPart(); FileDataSource fds = new FileDataSource("C:/text.txt"); attachmentPart.setDataHandler(new DataHandler(fds)); attachmentPart.setFileName("text.txt"); multipart.addBodyPart(attachmentPart); But this requires that the file reside somewhere on this disk. I would like to grab an outputstream right from the email library and stream file contents into it

How to write Strings to an OutputStream

喜你入骨 提交于 2019-12-01 03:06:14
How can I create class that takes an implentation of OutputStream and writes the the content to it? For example, the following print method is wrong and will not compile. What are the options or better techniques? public class FooBarPrinter{ private OutputStream o; public FooBarPrinter(OutputStream o){ this.o=o; } public void print(String s){ o.write(s); } } A generic OutputStream doesn't have the ability to write a String directly to it. Instead, you need to get the byte[] of the String and then write them out to the stream. public void print(String s){ o.write(s.getBytes()); } Refer to the

How do I use getOutputStream() and getWriter() in the same servlet request?

梦想的初衷 提交于 2019-12-01 01:05:25
How do I use getOutputStream() and getWriter() in the same servlet request? You can't use them both at the same time. If you first did getOutputStream() you can't consequently in the same request do getWriter() and vice versa. You can however wrap your ServletOuptputStream in a PrintWriter to get the same kind of writer you would have from getWriter() . ServletOutputStream out = response.getOutputStream(); // Notice encoding here, very important that it matches that of // response.setCharacterEncoding(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "utf-8")); Another

Handle Input using StreamGobbler

风格不统一 提交于 2019-11-30 20:53:17
I have been through the StreamGobbler at the following URL JavaWorld : Stream Gobbler I understand the usage and the reason on why it has been implemented. However the scenarios covered are only those wherein there could be an output from the command / handling error's. I do not find any scenario wherein StreamGobbler is used to handle inputs. For example, in mailx , I have to specify the body of the email, which I have done in the following format Process proc = Runtime.getRuntime().exec(cmd); OutputStreamWriter osw = new OutputStreamWriter(proc.getOutputStream()); osw.write(mailBody); osw

How do I use getOutputStream() and getWriter() in the same servlet request?

二次信任 提交于 2019-11-30 19:55:55
问题 How do I use getOutputStream() and getWriter() in the same servlet request? 回答1: You can't use them both at the same time. If you first did getOutputStream() you can't consequently in the same request do getWriter() and vice versa. You can however wrap your ServletOuptputStream in a PrintWriter to get the same kind of writer you would have from getWriter() . ServletOutputStream out = response.getOutputStream(); // Notice encoding here, very important that it matches that of // response

Java OutputStream Skip (offset)

泄露秘密 提交于 2019-11-30 19:34:44
I am trying to write a function that takes File object, offset and byte array parameters and writes that byte array to a File object in Java. So the function would look like public void write(File file, long offset, byte[] data) But the problem is that the offset parameter is long type, so I can't use write() function of OutputStream, which takes integer as an offset. Unlike InputStream, which has skip(long), it seems OutputStream has no way to skip the first bytes of the file. Is there a good way to solve this problem? Thank you. try { FileOutputStream out = new FileOutputStream(file); try {

How to put the content of a ByteBuffer into an OutputStream?

試著忘記壹切 提交于 2019-11-30 11:18:17
问题 I need to put the contents of a java.nio.ByteBuffer into an java.io.OutputStream . (wish I had a Channel instead but I don't) What's the best way to do this? I can't use the ByteBuffer's array() method since it can be a read-only buffer. I also may be interspersing writes to the OutputStream between using this ByteBuffer and having a regular array of byte[] which I can with use OutputStream.write() directly. 回答1: Look at Channels.newChannel(OutputStream). It will give you a channel given an

Java servlet and IO: Create a file without saving to disk and sending it to the user

南楼画角 提交于 2019-11-30 09:42:14
I`m hoping can help me out with a file creation/response question. I know how to create and save a file. I know how to send that file back to the user via a ServletOutputStream. But what I need is to create a file, without saving it on the disk, and then send that file via the ServletOutputStream. The code above explains the parts that I have. Any help appreciated. Thanks in Advance. // This Creates a file // String text = "These days run away like horses over the hill"; File file = new File("MyFile.txt"); Writer writer = new BufferedWriter(new FileWriter(file)); writer.write(text); writer