首先,解决两个小问题
1.输入中文乱码问题
一个小Demo即可解决:
package demo; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DemoServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置response查询的码表 //response.setCharacterEncoding("UTF-8"); //通过一个头 Content-Type 告知客户端使用何种码表 //response.setHeader("Content-Type", "text/html;charset=UTF-8"); //以上两行可以简写成这样,Tomcat自动设置编码 response.setContentType("text/html;charset=UTF-8"); PrintWriter writer = response.getWriter(); //writer.write("hello response!!!");写英文不需要考虑编码问题 writer.write("你好"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
2.怎么在客户端页面看到图片
用到字节流,示例如下:
package demo; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ByteServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 使用response获得字节输出流 ServletOutputStream out = response.getOutputStream(); // 获得服务器上的图片 String realPath = this.getServletContext().getRealPath("a.jpg"); InputStream in = new FileInputStream(realPath); // 字节数组提高效率 int len = 0; byte[] buffer = new byte[1024]; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } in.close(); out.close(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
接下来是文件下载案例:
WebContent目录下创建download文件夹,放入各种文件,以供下载
这里有两种方式下载:
1.<a>标签直接指向服务器资源,浏览器显示能解析的,解析不了的提供下载
这种方式只需要html代码即可(新建download.html)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>使用a标签指向服务器上的资源</h1> <a href="/WEB4/download/a.flv">a.flv</a> <br /> <a href="/WEB4/download/a.jpg">a.jpg</a> <br /> <a href="/WEB4/download/a.flv">a.mp3</a> <br /> <a href="/WEB4/download/a.mp4">a.mp4</a> <br /> <a href="/WEB4/download/a.txt">a.txt</a> <br /> <a href="/WEB4/download/a.zip">a.zip</a> <br /> <h1>使用服务器端编码的方式实现文件下载</h1> <a href="/WEB4/downloadServlet?filename=a.flv">a.flv</a> <br /> <a href="/WEB4/downloadServlet?filename=a.jpg">a.jpg</a> <br /> <a href="/WEB4/downloadServlet?filename=a.mp3">a.mp3</a> <br /> <a href="/WEB4/downloadServlet?filename=a.mp4">a.mp4</a> <br /> <a href="/WEB4/downloadServlet?filename=a.txt">a.txt</a> <br /> <a href="/WEB4/downloadServlet?filename=a.zip">a.zip</a> <br /> </body> </html>
2.使用服务器端编码的方式实现文件下载
package demo; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DownloadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获得要下载文件名称 String filename = request.getParameter("filename"); // 下载的文件类型(MIME类型) response.setContentType(this.getServletContext().getMimeType(filename)); // 告诉客户端该文件不是直接解析 而是以附件形式打开(下载) response.setHeader("Content-Disposition", "attachment;filename=" + filename); // 获取文件的绝对路径 String path = this.getServletContext().getRealPath("download/" + filename); // 获得该文件输入流 InputStream in = new FileInputStream(path); // 通过response获得输出流 ServletOutputStream out = response.getOutputStream(); // 文件拷贝 int len = 0; byte[] buffer = new byte[1024]; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } in.close(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
原文地址:https://www.cnblogs.com/xuyiqing/p/8408718.html
时间: 2024-11-13 07:56:25