congtouyue2015 2015-12-12 06:12 采纳率: 85%
浏览 1932
已采纳

出现下面错误怎么解决啊 ,求方法,请大神们帮我看看!

if(agent.contains("MSIE")){

这句报错, 错误提示The method contains(CharSequence) from the type String refers to the missing type CharSequence

 package cn.itcast.response;

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

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

/**
 * 文件下载
 * @author Administrator
 *
 */
public class DownloadServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 先获取到文件,读入输入流中
        // 获取文件的绝对磁盘路径
        String path = getServletContext().getRealPath("/img/外国美女.jpg");
        // 包含文件的名称    c:\tomcat\webapps\day10\img\girl7.jpg
        // 截取文件的名称    girl7.jpg
        String filename = null;
        // 获取最后一个\的位置
        int index = path.lastIndexOf("\\");
        if(index != -1){
            filename = path.substring(index+1);
        }

        // 判断是什么浏览器 Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
        // Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
        String agent = request.getHeader("User-Agent");
        // System.out.println(agent);
        if(agent.contains("MSIE")){
            filename = URLEncoder.encode(filename,"UTF-8");
        }

        if(filename != null){
            // 设置头信息
            response.setHeader("Content-Disposition", "attachment;filename="+filename);

            System.out.println(request.getRemoteAddr());

            System.out.println(path);
            InputStream in = new FileInputStream(path);
            // 通过response读出到客户端
            OutputStream os = response.getOutputStream();
            // io的拷贝
            byte [] b = new byte[1024];
            int len = 0;
            while((len =  in.read(b)) != -1){
                os.write(b, 0, len);
            }
            in.close();
            // os.close();服务器管理流
        }


    }

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

}

  • 写回答

3条回答 默认 最新

  • threenewbee 2015-12-12 06:58
    关注

    if(agent.contains("MSIE"))
    ->
    if(agent.indexOf("MSIE") != -1)
    这个支持更好

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?