[ Back | Previous | Next ]

How change name of file when downloading with a servlet?

Package:
javax.servlet.*
Product:
JSDK
Release:
1.x
Related Links:
General
General
General
General
General
Comment:
		String name = f.getName().substring(f.getName().lastIndexOf("/") + 1, f.getName().length());

		//System.out.println("-- " + name);
		String disposition = name + "\nContent-Disposition: attachment; filename=" + name + "\n\n";
		if (mimeType == null)
			res.setContentType("application/octet; name=" + name + "\nContent-Disposition: attachment; filename=" + name + "\n\n");
		else
			res.setContentType(mimeType + "; name=" + name + "\nContent-Disposition: attachment; filename=" + name + "\n\n");
		//
		//
		ServletOutputStream out = res.getOutputStream();
		//res.setContentLength((int) f.length() + disposition.length() + 1);
		//
		int bit = 256;
		int i = 0;
		try {
			while ((bit) >= 0) {
				bit = in.read();
				//System.out.println("" + bit);
				if (i++ % 1000 == 0)
					System.out.print("#");
				out.write(bit);
			}
			//System.out.println("" +bit);
		} catch (IOException ioe) {
			ioe.printStackTrace(System.out);
		}
		//System.out.println( "\n" + i + " bytes send.");
		//System.out.println( "\n" + f.length() + " bytes send.");
		out.flush();
		out.close();