Downloading a file thro Struts via response OutputStreaming
Hello there. Im making a application to download files. The problem im having is: Some files like PDF or DOC are downloaded correctly, but a .exe or a .dat or .txt are nawt. Im not really sure why is this happening, ive tryed many things. Heres the download part witch i belive is wrong somehow.
Code:
public void download(int indice) throws Throwable {
if(!validate()) throw new Exception("No response !");
response.setContentType("application/octet-stream");
String path = getDownloader().getDir() + getDownloader().getListaArquivos().get(indice); // example "C:/folder/file.txt"
File file = new File(path);
FileInputStream fileIn = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
byte[] outputByte = new byte[2*4096];
while(fileIn.read(outputByte, 0, 2*4096) != -1) {
out.write(outputByte, 0, 2*4096);
}
fileIn.close();
out.flush();
out.close();
}
Ive tryed to add this following line:
response.setHeader("Content-disposition", "attachment;"); , it does not work as well. If i add this in the begining of the page, it makes me download the .JSP, if i make this inside download method, it does nothing.
Does anyone knows how to solve this ?
Thanx for any responses.
[]´s Ziden