I am writing a server side class in smartGWT that fetches a record from a table and downloads a binary blob as a file from the database. The goal is to get the binary file attachment to download to local computer.
I was told that I could use DSRequest.getUploadedFile(attachment) to download the file but looking into it, it looks as though this method does not get an uploaded file from the database to download but gets a file to upload to the database.
Is there a way that I can get a file from the database and return it to the user as a download in server side code? We know we can do this in client side code but we would like to be able to do it on the server.
Here is how I am getting the record that contains the file I want to send to the user.
DSRequest dsReq = new DSRequest("table", "fetch");
dsReq.setCriteria("criteria", processFlowData.getVariableMap().get("critera"));
DataSource ds = dsReq.getDataSource();
DSResponse dsResp = ds.execute(dsReq);
if (dsResp.getStatus() < 0) {
//Handle Errors
} else {
if (!dsResp.getRecord().isEmpty()) {
//Download File Here
}
}
I am using Eclipse Kepler, SmartGWT, Java EE.
you could do something like this:
public static void downloadFile(DSRequest dsRequest, RPCManager rpcManager, HttpServletResponse servletResponse) throws Exception {
rpcManager.doCustomResponse();
ServletOutputStream outputStream = servletResponse.getOutputStream();
servletResponse.addHeader("Content-disposition", "attachment; filename=test.pdf");
servletResponse.setContentType("application/x-www-form-urlencoded");
OutputStream responseOutputStream = servletResponse.getOutputStream();
// write file to responseOutputStream
responseOutputStream.flush();
responseOutputStream.close();
}
Related
In my program I have a getRequest (JavaSpark) that offers me an Excel file created in Java for download.
To do this, I create the Excel file, save it in a folder and then read the file via the path.
Works.
Code - ExcelCreation:
public void createPlanWorkbook() throws Exception {
...
...
do something with workbook...
workBook.write("C:\\Users\\Develope\\Desktop\\Excel.xlsm");
}
Code - request:
get("/excelfile", ((request, response) -> {
response.header("Content-disposition", "attachment; filename=Excel.xlsm;");
File file = new File("C:\\Users\\Develope\\Desktop\\Excel.xlsm");
OutputStream outputStream = response.raw().getOutputStream();
outputStream.write(Files.readAllBytes(file.toPath()));
outputStream.flush();
return response;
}));
I wonder if there is another way to implement this. Is the step of saving and then reading the file necessary? Or is there a way to put the file directly from Javaobject into the request.
For example:
outputStream.write(ExcelCreaterObject().getWorkbook());
It seems that you are using Apache POI SmartXls to create an Excel workbook. Looking at its Javadoc, it seems that the Workbook.write method accepts an outputstream to write to.
So, you should be able to do write to the response stream directly, something like:
get("/excelfile", ((request, response) -> {
response.header("Content-disposition", "attachment; filename=Excel.xlsm;");
File file = new File("C:\\Users\\Develope\\Desktop\\Excel.xlsm");
OutputStream outputStream = response.raw().getOutputStream();
// do some stuff with the workbook
workbook.write(outputStream);
return response;
}));
I followed the discussion on spark github page as well as stack overflow to understand how to upload files using spark and apache file uploads.
Now I want the user to have an option to download the image on click.
For example my uploaded files get stored in /tmp/imageName.jpg on the server.
On the client side i want to give the user an option to download the file when the user clicks in the hyperlink.
click here
When the user click on the hyperlink I will call the function with the file path but can't understand how to send the image in response.
I do know that HTML5 has download attribute but that would require the files to be kept in public folder on the server which is not possible.
I went through the previous similar question add tried to replicate for my scenario without success
How can I send a PNG of a QR-code in a HTTP response body (with Spark)?
How download file using java spark?
Edit:
I did follow the link provided in the answer to force download the image, but using response.raw() i'm not able to get the response
response.type("application/force-download");
response.header("Content-Transfer-Encoding", "binary");
response.header("Content-Disposition","attachment; filename=\"" + "xxx\"");//fileName);
try {
HttpServletResponse raw = response.raw();
PrintWriter out = raw.getWriter();
File f= new File("/tmp/Tulips.jpg");
InputStream in = new FileInputStream(f);
BufferedInputStream bin = new BufferedInputStream(in);
DataInputStream din = new DataInputStream(bin);
while(din.available() > 0){
out.print(din.read());
out.print("\n");
}
}
catch (Exception e1) {
e1.printStackTrace();
}
response.status(200);
return response.raw();
Edit 2:
I'm not sure what is the difference between using response.body () vs response.raw().someFunction(). In either case I can seem to send the data back in response. Even if i write a simple response.body("hello") it doesn't reflect in my response.
Is there a difference in how a file would be read as opposed to an image ? Exampling using ImageIO class ?
Below is the solution that work for me:
Service.java
get(API_CONTEXT + "/result/download", (request, response) -> {
String key = request.queryParams("filepath");
Path path = Paths.get("/tmp/"+key);
byte[] data = null;
try {
data = Files.readAllBytes(path);
} catch (Exception e1) {
e1.printStackTrace();
}
HttpServletResponse raw = response.raw();
response.header("Content-Disposition", "attachment; filename=image.jpg");
response.type("application/force-download");
try {
raw.getOutputStream().write(data);
raw.getOutputStream().flush();
raw.getOutputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
return raw;
});
Angular Code
$scope.downloadImage= function(filepath) {
console.log(filepath);
window.open('/api/v1/result/download?filepath='+filepath,'_self','');
}
I am working on Java ExtJS application in which I need to create and download a CSV file.
On clicking a button I want a CSV file to be downloaded to a client's
machine.
On buttons listener I am calling a servlet using AJAX. There I am
creating a CSV file.
I don't want the CSV file to be saved in the server. I want the file should be created dynamically with a download option. I want the contents of a file to be created as a string and then I will serve the content as file in which it will open as download mode in browser (this I have achieved in other language, but not sure how to achieve it in Java).
Here is my code only to create a CSV file, but I really don't want to create or save CSV file if I can only download the file as CSV.
public String createCSV() {
try {
String filename = "c:\\test.csv";
FileWriter fw = new FileWriter(filename);
fw.append("XXXX");
fw.append(',');
fw.append("YYYY");
fw.append(',');
fw.append("ZZZZ");
fw.append(',');
fw.append("AAAA");
fw.append(',');
fw.append("BBBB");
fw.append('\n');
CSVResult.close();
return "Csv file Successfully created";
} catch(Exception e) {
return e.toString();
}
}
Can any one help me on this.
Thanks
I got the solution and I am posting it below.
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment; filename=\"userDirectory.csv\"");
try
{
OutputStream outputStream = response.getOutputStream();
String outputResult = "xxxx, yyyy, zzzz, aaaa, bbbb, ccccc, dddd, eeee, ffff, gggg\n";
outputStream.write(outputResult.getBytes());
outputStream.flush();
outputStream.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
Here we don't need to save / store the file in the server.
Thanks
First of all you need to get the HttpServletResponse object so that you can stream a file into it.
Note : This example is something I Wrote for one of my projects and it works.Works on Java 7.
Assuming you got the HttpServletResponse you can do something like this to stream a file. This way the file will be saved into clients' machine.
public void downloadFile(HttpServletResponse response){
String sourceFile = "c:\\source.csv";
try {
FileInputStream inputStream = new FileInputStream(sourceFile);
String disposition = "attachment; fileName=outputfile.csv";
response.setContentType("text/csv");
response.setHeader("Content-Disposition", disposition);
response.setHeader("content-Length", String.valueOf(stream(inputStream, response.getOutputStream())));
} catch (IOException e) {
logger.error("Error occurred while downloading file {}",e);
}
}
And the stream method should be like this.
private long stream(InputStream input, OutputStream output) throws IOException {
try (ReadableByteChannel inputChannel = Channels.newChannel(input); WritableByteChannel outputChannel = Channels.newChannel(output)) {
ByteBuffer buffer = ByteBuffer.allocate(10240);
long size = 0;
while (inputChannel.read(buffer) != -1) {
buffer.flip();
size += outputChannel.write(buffer);
buffer.clear();
}
return size;
}
}
What this does is, get an inputstream from your source file and write that stream into the outputstream of the HttpServletResponse. This should work since it works perfectly for me. Hope this helps. Sorry for my bad English.
I would like add something to the answer by gaurav. I recently had to implment this functionality in a project of mine and using javascript was out of the question becuase we had to support IE 9. What is the problem with IE 9?
(Export to CSV using jQuery and html), see the second answer in the link.
I needed an easy way to convert a ResultSet of a database query to a string which represent the the same data in CSV format. For that I used http://opencsv.sourceforge.net/ which provided an easy way to get a String ot of the ResultSet, and the rest is as above answer did it.
THe examples in the project soruce folder give good examples.
I have to create a CSV file on a nightly process and avail those files to client as an http download.
I have created a CSV with the help PLSQL block as:
DECLARE
F UTL_FILE.FILE_TYPE;
CURSOR C1 IS SELECT emp_id, name FROM employee;
C1_R C1%ROWTYPE;
BEGIN
F := UTL_FILE.FOPEN('EXPORT_DIR','employee.csv','w',32767);
FOR C1_R IN C1
LOOP
UTL_FILE.PUT(F,C1_R.emp_id);
UTL_FILE.PUT(F,','||C1_R.name);
UTL_FILE.NEW_LINE(F);
END LOOP;
UTL_FILE.FCLOSE(F);
END;
/
this will create a CSV file "employee.csv" on the database server. Now I have to avail this file to the clients as a HTTP downloadable file. How to do that ?
You can provide one Servlet to download your file as a csv format.
DoanloadServlet.java :
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
//set content type to csv and response as attachment
response.setHeader("Content-Type", "text/csv");
response.setHeader("Content-Disposition", "attachment; filename=Myfile.csv");
OutputStream out = response.getOutputStream();
//read your file from database in outputstream
FileInputStream in = new FileInputStream(my_file);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
catch(Exception e)
{
//Exception handling
}
}
Give user a link and call this servelet with required parameter to find file from database.
You link look like this
http://serveraddress.com/DoanloadServlet?fileName=name_of_file
I found it easy to copy the generated file web server. we can just use SCP or FTP to the generated file on webserver. e.g.
scp /mnt/csv_dir/feed.csv root#server:/home/stackoverflow/www/csv
Add this command in a nightly process to run
The above command will move the feed.csv from csv_dir to csv folder of web server.
now we will be able to access the file using the web url.
I'm using following uploader - http://valums.com/ajax-upload/ . It calls method where I get MultipartFile. The question is: how can I detect the situation that the file is not correctly uploaded - user canceled uploading or something else?
The sample upload server code (it's groovy)
`def upload = {
File file = createTemporaryFile()
InputStream inputStream = selectInputStream(request)
file << inputStream
}
private InputStream selectInputStream(HttpServletRequest request) {
if (request instanceof MultipartHttpServletRequest) {
MultipartFile uploadedFile = ((MultipartHttpServletRequest) request).getFile('qqfile')
return uploadedFile.inputStream
}
return request.inputStream
}`
Im not sure if there's a 'standard' way, but why not transmit a hash with the file?
Once upload is complete, calculate the hash on the server and see if it matches.