tom
tom

Reputation: 93

Servlet java, server side, send a string as a new txt file

I have a Java web app. The user is able to upload a file (via POST request), and then the servlet will execute some code on this file. The output of this code is a string.

How to return a file to the user, without create one on the server side? Is that possible?

Upvotes: 0

Views: 1599

Answers (2)

RamonBoza
RamonBoza

Reputation: 9038

Try to use that code

package com.hainasoft.web;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownloadServlet extends javax.servlet.http.HttpServlet implements
        javax.servlet.Servlet {
    static final long serialVersionUID = 1L;
    private static final int BUFSIZE = 4096;
    private String filePath;

    public void init() {
        // the file data.xls is under web application folder
        filePath = getServletContext().getRealPath("") + File.separator + "data.xls";
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        File file = new File(filePath);
        int length   = 0;
        ServletOutputStream outStream = response.getOutputStream();
        ServletContext context  = getServletConfig().getServletContext();
        String mimetype = context.getMimeType(filePath);

        // sets response content type
        if (mimetype == null) {
            mimetype = "application/octet-stream";
        }
        response.setContentType(mimetype);
        response.setContentLength((int)file.length());
        String fileName = (new File(filePath)).getName();

        // sets HTTP header
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

        byte[] byteBuffer = new byte[BUFSIZE];
        DataInputStream in = new DataInputStream(new FileInputStream(file));

        // reads the file's bytes and writes them to the response stream
        while ((in != null) && ((length = in.read(byteBuffer)) != -1))
        {
            outStream.write(byteBuffer,0,length);
        }

        in.close();
        outStream.close();
    }
}

it is important to set the myme type

mimetype = "application/octet-stream";

and with this you set a download dialog to the user

response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

I have retrieved all that information from http://www.java-forums.org/blogs/servlet/668-how-write-servlet-sends-file-user-download.html

so take a look in it for more accurate information.

Upvotes: 0

Pankaj Sharma
Pankaj Sharma

Reputation: 1853

yes, you have to set content disposition header then write the stream to response.

Upvotes: 1

Related Questions