HTTP响应头信息 Content-Type

Content-Type用于定义网络文件的类型和网页的编码,决定浏览器采用何种方式对响应体进行处理。HttpServletRequest提供了一个专门的方法setContentType()来设置Content-Type

MIME类型 描述
text/html HTML格式
text/plain 纯文本格式
text/xml XML格式
image/jpg jpg图片格式
image/png png图片格式
image/git git图片格式
multipart/form-data 表单进行文件上传时使用的格式
application/octet-stream

application/x-msdownload

Content-Disposition (attachment / inline)
二进制流数据(如常见的文件下载)
. . . . . . . . . . . .

HTML文档实例
/**
 * @Author: 落叶无痕
 * @Date: 2020/6/2 17:03
 */
@WebServlet("/ct")
public class UserAgentServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	//设置Content-Type内容类型
        response.setContentType("text/html;charset=utf-8");
        //浏览器打印输出
        response.getWriter().println("<h1><a href='http://caidong4356.top/'>Caidong的个人博客</a></h1>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doPost(request, response);
    }

}

cd4356


纯文本实例
/**
 * @Author: 落叶无痕
 * @Date: 2020/6/2 17:03
 */
@WebServlet("/ct")
public class UserAgentServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	//设置Content-Type内容类型
        response.setContentType("text/plain;charset=utf-8");
        //浏览器打印输出
        response.getWriter().println("<h1><a href='http://caidong4356.top/'>Caidong的个人博客</a></h1>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doPost(request, response);
    }

}

cd4356


文件下载实例
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @Author: 落叶无痕
 * @Date: 2020/6/3 16:32
 */
@Controller
public class UploadController {

    @GetMapping("/download/{filename}")
    public void downloadFile(@PathVariable("filename") String filename, HttpServletResponse response) throws UnsupportedEncodingException {

        if(filename != null){
            //获取要下载的文件对象
            File file = new File("D:/jianli/", filename);
            if(file.exists()){
				//以二进制流的形式下载文件(这样可以实现任意格式的文件下载)
                response.setContentType("application/x-msdownload");
             // response.setContentType("application/octet-stream);

                InputStream input = null;
                OutputStream output = null;
                try{
                    input = new FileInputStream(file); //创建文件输入流对象
                    output = response.getOutputStream(); //创建文件输出流对象
                    byte[] buf = new byte[1024]; //定义一个字节数组作为缓冲区,并指定一次读取1024个字节
                    int len; //记住读入缓冲区的字节长度
                    while ((len = input.read(buf)) != -1){ //判断文件是否读完(读完后会返回-1)
                        //注意不要用output.write(buf); 否则可能导致最终写入的文件比原文件大,如果文件是图片的话,那么就会失真
                        output.write(buf, 0, len); //从buf缓冲区中读取数据,从第一个字节开始读,一次读取len,并将读取出来的数据写入文件输出流对象中
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    //最后记得关闭IO流
                    try {
                        input.close();
                        output.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }


}

cd4356

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章