// 像servlet一样使用HttpServletRequest和HttpServletResponse @RequestMapping("/sev") public String sev(HttpServletRequest request, HttpServletResponse response) { String id = request.getParameter("id"); response.setContentType("text/html;charset=utf-8"); try { PrintWriter out = response.getWriter(); out.write(id); } catch (IOException e) { e.printStackTrace(); } return null; }
public void serializeHistoryCursor( Collection<TrackHistory> historyCursor, HttpServletResponse httpServletResponse) { try { final ServletOutputStream httpOutputStream = httpServletResponse.getOutputStream(); final BufferedWriter outputStream = new BufferedWriter(new OutputStreamWriter(httpOutputStream)); outputStream.write("{"); outputStream.write("\"count\":"); outputStream.write("" + historyCursor.size()); if (historyCursor.size() > 0) { Gson gson = new Gson(); outputStream.write(","); outputStream.write("\"tracks\":["); for (Iterator<TrackHistory> iterator = historyCursor.iterator(); iterator.hasNext(); ) { TrackHistory next = iterator.next(); outputStream.write(gson.toJson(toWebTrack(next))); if (iterator.hasNext()) { outputStream.write(","); } outputStream.flush(); } /* while (historyCursor.hasNext()) { outputStream.write(gson.toJson(toWebTrack(historyCursor.next()))); if (historyCursor.hasNext()) { outputStream.write(","); } outputStream.flush(); } */ outputStream.write("]"); } outputStream.write("}"); outputStream.flush(); outputStream.close(); httpOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
@RequestMapping("/upfile") @ResponseBody public String upfile( @RequestParam("desc") String desc, @RequestParam("file") MultipartFile file) { try { if (!file.isEmpty()) { logger.info("{}上传中", desc); file.transferTo(new File("d:/fileupload/" + file.getOriginalFilename())); return "上传成功"; } } catch (IllegalStateException e) { logger.error("非法状态错误"); e.printStackTrace(); } catch (IOException e) { logger.error("文件I/O错误"); e.printStackTrace(); } return "上传失败"; }