inf : http://www.mkyong.com/servlet/servlet-code-to-download-text-file-from-website-java/
download file
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDownloadDemo extends HttpServlet{ private static final int BYTES_DOWNLOAD = 1024; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ response.setContentType("text/plain"); response.setHeader("Content-Disposition", "attachment;filename=downloadname.txt"); ServletContext ctx = getServletContext(); InputStream is = ctx.getResourceAsStream("/testing.txt"); int read=0; byte[] bytes = new byte[BYTES_DOWNLOAD]; OutputStream os = response.getOutputStream(); while((read = is.read(bytes))!= -1){ os.write(bytes, 0, read); } os.flush(); os.close(); } }
沒有留言:
張貼留言