Spring学习笔记1


  1. RequestAttributes ra = RequestContextHolder.getRequestAttributes();  
  2. //获取resquest对象
  3. HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest();  
  4. //获取session()
  5. request.getSession().setAttribute("sessionMessage""im'sessionMessage!"); 
  6. //获取文件路径 
  7. String serverRealPath = application.getRealPath("/file");
  8. //restful 初探
  9. @RequestMapping("/file")
    @Controller
    public class FileHandler {


    public FileHandler() {
    System.out.println("file create ");
    }


    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public KitFile create(@RequestParam String type, @RequestParam MultipartFile[] files) {


    System.out.println();
    System.out.println("sdsd");
    return new KitFile();
    }


    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String show(@PathVariable String id) {


    System.out.println("sdsd");
    return "ss";
    }


    @ResponseBody
    @RequestMapping(value = "/{id}/{str}", method = RequestMethod.GET)
    public KitFile index(@PathVariable("id") String id, @PathVariable("str") String str) {
    System.out.println("sdsd" + id);
    return new KitFile();
    }


    @ResponseBody
    @RequestMapping(value = "/{id}/{str}", method = RequestMethod.PUT)
    public KitFile update(@PathVariable("id") String id) {
    System.out.println("sdsd" + id);
    return new KitFile();
    }


    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public KitFile delete(@PathVariable("id") String id) {
    System.out.println("sdsd" + id);
    return new KitFile();
    }
    }
  10. public void testAdd() throws Exception {
    // HttpPost httpPost = new HttpPost("http://localhost:8080/springmvc-2/file");
    HttpPost httpPost = new HttpPost("http://localhost:8080/ifile/file");
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    CloseableHttpClient client = HttpClients.createDefault();
    try {
    builder.setCharset(Charset.forName("utf-8"));
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);// 设置浏览器兼容模式
    FileBody fileBody = new FileBody(new File("d:\\test\\3.jpg"));
    FileBody fileBody2 = new FileBody(new File("d:\\test\\2.jpg"));


    //  builder.addPart("file", fileBody);
     builder.addPart("files", fileBody2);
     
    // builder.addTextBody("_id", "1asdasd", ContentType.APPLICATION_JSON);//添加不了
    builder.addTextBody("type", "apk", ContentType.DEFAULT_TEXT);
    // builder.addTextBody("name", "aaa", ContentType.APPLICATION_JSON);
    // builder.addTextBody("describle", "你好", ContentType.APPLICATION_JSON);
    // builder.addTextBody("time", "2012-02-20 12:11:33", ContentType.APPLICATION_JSON);

    HttpEntity entity = builder.build();


    httpPost.setEntity(entity);
    String retData = "";
    HttpResponse response = client.execute(httpPost);
    if (response.getStatusLine().getStatusCode() == 200) {
    HttpEntity httpEntity = response.getEntity();
    if (httpEntity != null) {
    retData = EntityUtils.toString(httpEntity);
    System.out.println(retData);
    }
    } else {
    throw new Exception("Connected Exception");
    }


    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    client.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
  1. public void testget() throws Exception {
    HttpClient client = new DefaultHttpClient();
    // HttpGet httpGet = new HttpGet("http://localhost:8080/springmvc-2/file/safsad");
    HttpGet httpGet = new HttpGet("http://localhost:8080/ifile/file/22");
    // HttpGet httpGet = new HttpGet("http://localhost:8080/ifile/file/22/ss=111");
    HttpResponse response = client.execute(httpGet);
    client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
    client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);
    StringBuilder builder = new StringBuilder();
    if (response.getStatusLine().getStatusCode() == 200) {
    // 获取响应内容
    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    for (String s = reader.readLine(); s != null; s = reader.readLine()) {
    builder.append(s);
    }
    }
    httpGet.abort();
    System.out.println(builder.toString());
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值