请求返回json串和文件下载实例

请求返回json串

JsonServlet代码:

@WebServlet("/JsonServelt")
public class JsonServelt extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public JsonServelt() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        response.setContentType("text/json;charset=utf-8");
        //采用字符集编码方式
        PrintWriter out = response.getWriter();
        //获取json数据
        JSONArray array = MyUtils.getJSONArray();
        //输出json数据
        out.write(array.toString());

    }

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

}

MyUtils类:

public class MyUtils {
    public static JSONArray getJSONArray() {

        List<User> listUsers = new ArrayList<>();

        listUsers.add(new User("moly",18,2,true,"good kid"));
        listUsers.add( new User("lili",19,3,false,"kid"));
        listUsers.add(new User("lucy",18,2,false,"good kid"));
        listUsers.add(new User("joy",16,2,false,"good kid"));
        listUsers.add(new User("bob",17,2,true,"kid"));

        JSONArray array = new JSONArray();
        JSONObject object= null;

        for (User user : listUsers) {
            object = new JSONObject();
            try {
                object.put("name", user.name);
                object.put("age", user.age);
                object.put("level", user.level);
                object.put("sex", user.sex);
                object.put("desc", user.desc);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            array.put(object);
        }
        return array;
    }
}

web.xml配置信息

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
    ...
    <!-- 输出json的Servlet -->
    <servlet>
        <servlet-name>JsonServelt</servlet-name>
        <servlet-class>com.yundoku.JsonServelt</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>JsonServelt</servlet-name>
        <url-pattern>/json</url-pattern>
    </servlet-mapping>
    ...
</web-app>

文件下载

DownloadServlet代码:

@WebServlet("/DownloadServlet")
public class DownloadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public DownloadServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        response.setContentType("image/jpeg;charset=utf-8");

        //获取文件路径
        String realPath = getServletContext().getRealPath("/李冰冰.jpg");

        File file = new File(realPath);

        //设置下载文件的头信息
        response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(file.getName(),"utf-8"));

        //创建一个文件字节流
        FileInputStream fis = new FileInputStream(file);

        //获取输出流
        ServletOutputStream sos = response.getOutputStream();

        //io流操作
        int len = 0;
        byte[] bytes = new byte[1024];
        while((len=fis.read(bytes))>0){
            sos.write(bytes, 0, len);
        }
        //关闭流操作
        sos.flush();
        sos.close();
        fis.close();

    }

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

}

web.xml配置:

<servlet>
    <servlet-name>DownloadServlet</servlet-name>
    <servlet-class>com.yundoku.DownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>DownloadServlet</servlet-name>
    <url-pattern>/download</url-pattern>
</servlet-mapping>
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 7 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐