IE8 asks to Open/Save twice on .xls file - java

I'm generating an Excel document via Servlet. When I send the response back to the client (IE8), the "Open/Save" dialog pops up but requires users to click a choice twice before taking action. This doesn't happen in Firefox. I have no idea why this is occurring. Below is the relevant code that creates the appropriate streams.
result contains the Excel XML.
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=TestFile.xls");
InputStream in = new ByteArrayInputStream(result.toString().getBytes("UTF-8"));
ServletOutputStream out = response.getOutputStream();
try
{
byte[] outputByte = new byte[4096];
while(in.read(outputByte, 0, 4096) != -1)
out.write(outputByte, 0, 4096);
}
finally
{
in.close();
out.flush();
out.close();
}
EDIT
I have noticed that waiting 5+ seconds before clicking an option works just fine. It seems to only ask twice when immediately clicking an option.

This code works well for every type of file in my application
InputStream in = blob.getBinaryStream();
// Output the blob to the HttpServletResponse
String codedfilename = "";
//this code resolves the issue with the encoding of the downloaded filename
String agent = request.getHeader("USER-AGENT");
if (null != agent && -1 != agent.indexOf("MSIE"))
{
codedfilename = URLEncoder.encode(/*here goes the filename*/, "UTF8");
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename=" + codedfilename);
}
else if (null != agent && -1 != agent.indexOf("Mozilla"))
{
response.setCharacterEncoding("UTF-8");
//It does not seem to make a difference whether Q or B is chosen
codedfilename = MimeUtility.encodeText(rset.getString("FILE_NAME"), "UTF8", "B");
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment; filename=\"" + codedfilename + "\"");
}
BufferedOutputStream out =
new BufferedOutputStream(response.getOutputStream());
byte by[] = new byte[32768];
int index = in.read(by, 0, 32768);
while (index != -1) {
out.write(by, 0, index);
index = in.read(by, 0, 32768);
}
out.flush();
try it and let us know

Related

In Groovy, how to properly get the file from HttpServletRequest

I am writing a REST API in Groovy script that will receive a file upload from client side.
The REST API will receive the file via HttpServletRequest.
I am trying to get the file from HttpServletRequest by getting its InputStream, then convert it to File to save to proper folder.
My code is as below:
RestApiResponse doHandle(HttpServletRequest request, RestApiResponseBuilder apiResponseBuilder, RestAPIContext context) {
InputStream inputStream = request.getInputStream()
def file = new File(tempFolder + "//" + fileName)
FileOutputStream outputStream = null
try
{
outputStream = new FileOutputStream(file, false)
int read;
byte[] bytes = new byte[DEFAULT_BUFFER_SIZE];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
}
finally {
if (outputStream != null) {
outputStream.close();
}
}
inputStream.close();
// the rest of the code
}
The files are created, but all of them are corrupted.
When I try to open them with Notepad, all of them have, at the beginning, some thing similar to the below:
-----------------------------134303111730200325402357640857
Content-Disposition: form-data; name="pbUpload1"; filename="Book1.xlsx"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Am I doing this wrong? How do I get the file correctly?
Found the solution with MultipartStream
import org.apache.commons.fileupload.MultipartStream
import org.apache.commons.io.FileUtils
InputStream inputStream = request.getInputStream()
//file << inputStream;
String fileName = "";
final String CD = "Content-Disposition: "
MultipartStream multipartStream = new MultipartStream(inputStream, boundary);
//Block below line because it always return false for some reason
// but should be used as stated in document
//boolean nextPart = multipartStream.skipPreamble();
//Block below line as in my case, the part I need is at the first part
// or maybe I should use it and break after successfully get the file name
//while(nextPart) {
String[] headers = multipartStream.readHeaders().split("\\r\\n")
ContentDisposition cd = null
for (String h in headers) {
if (h.startsWith(CD)) {
cd = new ContentDisposition(h.substring(CD.length()));
fileName = cd.getParameter("filename"); }
}
def file = new File(tempFolder + "//" + fileName)
ByteArrayOutputStream output = new ByteArrayOutputStream(1024)
try
{
multipartStream.readBodyData(output)
FileUtils.writeByteArrayToFile(file, output.toByteArray());
}
finally {
if (output != null) {
output.flush();
output.close();
}
}
inputStream.close();

remove or not return BufferedOutputStream file in java

i would like not to download the BufferedOutputStream when return java method.
my code:
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
response.setHeader("Content-Disposition", "attachment; filename=\"" + "Invoice.zip\";");
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
ZipOutputStream zos = new ZipOutputStream(bos);
for(SalesEInvObject InvoiceObj : this.InvoiceTable){ // MAIN FOR-LOOP STARTS
if (InvoiceObj.getInvoiceNo() != null) {
javax.servlet.http.HttpSession httpSession =(javax.servlet.http.HttpSession) ctx.getExternalContext().getSession(false);
httpSession.setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,
reportOutput.getInternalReportObject());
byte[] bytes = reportOutput.getReportOutputBytes();
int length = ((bytes == null) ? 0 : bytes.length);
response.setContentLength(length*tableSize);
final ZipEntry ze = new ZipEntry(reportOutputFileName+".pdf");
zos.putNextEntry(ze);
zos.write(bytes, 0, bytes.length);
zos.closeEntry();
}else {
return null;
}
}//LOOP ENDS
zos.close();
ctx.responseComplete();
my problem is when the invoices has Number it generates invoice and download in compressed zip file. but when it has no Number i dont want to download zip. but still zip file downloads but with empty no file in it.
if no pdf generated i dont want to download zip file.
any help...
Once you have started generating and writing the ZIP to the response output stream, there is no turning back. Just opening the stream causes the response to "commit" ... meaning that you can no longer change the response code or headers.
Basically, you need to check if there are any invoices before you start generating the response. Then it should just be a matter of reorganizing the existing code.
Something like .....
boolean hasInvoices = false;
for (SalesEInvObject invoiceObj : this.InvoiceTable) {
if (invoiceObj.getInvoiceNo() != null) {
hasInvoices = true;
break;
}
}
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletResponse response =
(HttpServletResponse) ctx.getExternalContext().getResponse();
if (hasInvoices) {
response.setHeader("Content-Disposition",
"attachment; filename=\"" + "Invoice.zip\";");
BufferedOutputStream bos =
new BufferedOutputStream(response.getOutputStream());
ZipOutputStream zos = new ZipOutputStream(bos);
for (SalesEInvObject invoiceObj : this.InvoiceTable) {
if (invoiceObj.getInvoiceNo() != null) {
javax.servlet.http.HttpSession httpSession =
(javax.servlet.http.HttpSession) ctx.getExternalContext()
.getSession(false);
httpSession.setAttribute(
BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,
reportOutput.getInternalReportObject());
byte[] bytes = reportOutput.getReportOutputBytes();
int length = ((bytes == null) ? 0 : bytes.length);
response.setContentLength(length * tableSize);
final ZipEntry ze = new ZipEntry(reportOutputFileName + ".pdf");
zos.putNextEntry(ze);
zos.write(bytes, 0, bytes.length);
zos.closeEntry();
}
}
zos.close();
} else {
// do you want to set a response code or something?
}
ctx.responseComplete();
I have fixed some bad style. See if you can spot the changes ...
There is another problem that I haven't addressed: namely that the various resources that are opened in this code ought to be managed using try with resources. However, it may not be necessary since it looks like the resources are all based on with the request output stream. That will be closed automatically by the servlet infrastructure.

Filetransfer over Socket in Java, wont go out of while-loop

I am trying to create a simple server-client-program where the user can upload and download files. I have got the Sockets and Streams to work, and I can upload a file to the server. But whenever one file has been uploaded the Server-side seems to get stuck in the loop that reads the streams and forwards it to the Server-file.
Server Code:
InputStream in = clientSocket.getInputStream();
String filePath = "......."
+ op[1];
System.out.println(op[0] + ": " + filePath);
FileOutputStream out = new FileOutputStream(filePath);
byte[] bytes = new byte[16*1024];
int count;
while ((count = in.read(bytes)) > 0) {
out.write(bytes, 0, count);
}
Client Code:
String filePath = "...."
+ path;
System.out.println("Attempting: " + filePath);
dos = new DataOutputStream(serverSocket.getOutputStream());
fis = new FileInputStream(filePath);
byte[] buffer = new byte[4096];
while (fis.read(buffer) > 0) {
dos.write(buffer);
}
dos.flush();
fis.close();
The problem is that the program gets stuck at the while-loop, so the Server can not perform anything else. There are no errors or anything...
You never close the stream on the client side. Add dos.close() after dos.flush()!

Servlet writing file contains binary at the end

I have a servlet to let user download a file from server. The original file is human readable, but the downloaded file alwyas contains binary content at the end of the file.
HttpSession session = request.getSession(true);
String fileName = session.getAttribute("download").toString();
System.out.println("Download file " + fileName);
File file = new File(fileName);
FileInputStream fileIn = new FileInputStream(file);
response.setContentType("text/plain");
response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
ServletOutputStream out = response.getOutputStream();
byte[] bytes = new byte[BYTES_DOWNLOAD];
while (fileIn.read(bytes, 0, BYTES_DOWNLOAD) != -1) {
out.write(bytes, 0, BYTES_DOWNLOAD);
}
out.flush();
out.close();
Thanks in advance.
Small bug in your code:
byte[] bytes = new byte[BYTES_DOWNLOAD];
int count;
while ( (count = fileIn.read(bytes)) != -1) {
out.write(bytes, 0, count);
}
What binary content do you see? Is it perhaps the line endings in the file, if any?

What is the accurate way to read/download a file from the server (in Java)?

In my client-server application I have used a command (GET filename)to download a file into the client side. I have used the build in read() method to read the file. My teachers said it's not a very good practice to implement this read method. The reason is either it doesn't tell how exactly the file is reading from the server or it somehow is not able to download dynamic (large) file size. But at the moment I see that it's working fine. Since I am still in intermediate level in java, I need to learn the best way to do this job. How it could be improved in coding? That is I want to improve the while looping part in ClientSide.
I have pasted the relevent code:
ClientSide:
............
............
if (request.startsWith("GET")) {
File file = new File(request.substring(4));
is = socket.getInputStream();
fos = new FileOutputStream(file);
byte[] buffer = new byte[socket.getReceiveBufferSize()];
int bytesReceived = 0;
while ((bytesReceived = is.read(buffer)) >=0) {
//while ((bytesReceived = is.read(buffer))>=buffer) {
fos.write(buffer, 0, bytesReceived);
}
request = "";
fos.close();
is.close();
}
.................
.................
ServerSide:
.................
.................
else if (request.startsWith("GET")) {
System.out.println("");
String filename = request.substring(4);
File file = new File(System.getProperty("user.dir"));
File[] files = file.listFiles();
if (fileExists(files, filename)) {
file = new File(filename);
int fileSize = (int) file.length();
outputToClient.print("Status OK\r\n"
+ "Size " + fileSize + "KB" + "\r\n"
+ "\r\n"
+ "File " + filename + " Download was successfully\r\n");
outputToClient.flush();
// reading files
fis = new FileInputStream(file);
os = socket.getOutputStream();
byte[] buffer = new byte[2^7-1];
int bytesRead = 0;
while ((bytesRead = fis.read(buffer))!= -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
fis.close();
} else {
outputToClient.print("Status 400\r\n"
+ "File " + filename + " not found\r\n"
+ "\r\n");
outputToClient.flush();
}
}
outputToClient.flush();
}
.................
.................
You need to consume the rest of the HTTP response headers, by reading until you get a blank line, if you haven't already done that.
Apart from that, your code looks fine to me, except that I would use a much bigger buffer than 127, at least 8192, possibly a multiple of that.
Ask your teacher what (on earth) he's talking about.

Categories

Resources