# Datei vom Server downloaden



## MQue (5. Okt 2009)

Hallo,

ich möchte nach einer Anfrage des Clients an den Server, dass sich ein Download- Fenster öffnet, Wie kann ich das machen,
ich hab folgendes ohne Erfolg versucht,
Meines Wissens müsste das ja über den MIME- Type gehen, oder?


```
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/octet-stream");
        final PrintWriter out = response.getWriter();
        try {
            out.println("Daten vom Server"); // am Client soll ein DownloadFenster erscheinen,
// mit welchen ich eine Datei speichern kann, wo dann eben "Daten vom Server" drinnensteht
            }
        finally {
            out.close();
            }
        }
```


----------



## velaluka (5. Okt 2009)

Hallo,
schnell einen Schnipsel...

```
/**
	 * {@inheritDoc}
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Object obj = request.getAttribute("currentFormObject");
		VendorCommand vendorCommand = null;
		ServletOutputStream out = response.getOutputStream();
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			if (obj != null) {
				vendorCommand = (VendorCommand) obj;
				Blob rawData = null;

				conn = getDataSource().getConnection();
				stmt = conn.createStatement();
				rs = stmt.executeQuery("select raw_data from vm_file where  id = " + vendorCommand.getFileId());
				if (rs.next()) {
					rawData = rs.getBlob(1);
				}
				// Try to get the current vendorCommand
				InputStream in = rawData.getBinaryStream();

				int length = (int) vendorCommand.getFileSize();
				response.setContentType("application/force-download");
				response.setHeader("Content-Disposition", "attachment; filename=\"" + vendorCommand.getFileName() + "\"");
				response.setHeader("Content-Length" , String.valueOf(length));
				response.setHeader("Cache-Control", "max-age=0");
				response.setHeader("Pragma", "public");
				int bufferSize = BUFFER_SIZE;
				byte[] buffer = new byte[bufferSize];

				while ((length = in.read(buffer)) != -1) {
					out.write(buffer, 0, length);
				}
			}
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
			out.println("<html><head><title>File Download</title></head>");
			out.println("<body><h1>Database Connection Problem.</h1></body></html>");
			return;
		} finally {
			try {
				if (rs != null) {

					rs.close();

				}
				if (stmt != null) {
					stmt.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				log.error("", e);
			}
		}
	}
```
Hoffe es hilft....
Ciao velaluka


----------



## MQue (5. Okt 2009)

funktioniert, Besten Dank!


----------

