java--超鏈接下載

package com.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Demo_1 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String filename =request.getParameter("filename");
	String path = getServletContext().getRealPath("download/filename");
	InputStream is=new FileInputStream(path);
	OutputStream os=response.getOutputStream();
	int len =0;
	byte[] buffer=new byte[1024];
	while((len =is.read(buffer)) != -1) {
		os.write(buffer,0,len);
	}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	doGet(request, response);
}

}

你可能感兴趣的:(java--超鏈接下載)