需要的jar包:commons-fileupload-1.3.1.jar
示例代码:
package com.expai.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import com.expai.util.JDBCUtil; public class PhotoDataServlet extends HttpServlet{ private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{ doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{ response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); JDBCUtil dbUtil = new JDBCUtil(); Integer number = 0; Integer pageNum = 0; // String size = null; if(request.getParameter("number")!=null){ number = Integer.parseInt(request.getParameter("number")); } if(request.getParameter("pageNum")!=null){ pageNum = Integer.parseInt(request.getParameter("pageNum")); } // if(request.getParameter("size")!=null){ // size = new String(request.getParameter("size").getBytes("ISO-8859-1"), "UTF-8"); // } if(number!=null&&pageNum!=null){ // String sql = "select id,photoUrl from tb_photo_data where userId = "+userId +" limit "+Integer.parseInt(pageNum)*Integer.parseInt(number)+","+number; String sql = "select id,photoUrl,width,height from tb_photo_data limit "+pageNum*number+","+number; ArrayList<HashMap<String,Object>> listData = dbUtil.getDataListMapNew(sql); PrintWriter pWriter = response.getWriter(); JSONArray json = JSONArray.fromObject(listData); pWriter.flush(); pWriter.print(json); } } }
fileupload form表单提交(包括文件上传)
时间: 2024-09-28 03:37:09