Spring学习笔记1
2015-06-02 13:27
483 查看
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
//获取resquest对象
HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest();
//获取session()
request.getSession().setAttribute("sessionMessage", "im'sessionMessage!");
//获取文件路径
String serverRealPath = application.getRealPath("/file");
//restful 初探
@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();
}
}
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();
}
}
}
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());
}
//获取resquest对象
HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest();
//获取session()
request.getSession().setAttribute("sessionMessage", "im'sessionMessage!");
//获取文件路径
String serverRealPath = application.getRealPath("/file");
//restful 初探
@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();
}
}
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();
}
}
}
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());
}
相关文章推荐
- java事件处理机制
- java基础(一)
- Java Bean validation specification...
- Java多线程系列--“JUC原子类”02之 AtomicLong原子类
- java通过反射 寻找配置文件 调用方法
- Java多线程系列--“JUC原子类”01之 框架
- Javabean+servlet+JSP(html)实例应用
- java设计模式(二)---单例模式
- 使用Tortoise SVN版本控制Eclipse工作区的Android项目
- JAVA定时执行任务,每天定时几点钟执行任务
- Eclipse导入到web项目没有run on server
- hdu 折线分割平面 (java)
- hdu 神、上帝以及老天爷 java
- hdu 阿牛的EOF牛肉串 (java)
- hdu 骨牌铺方格 (java)
- 获取java类中所有方法及其参数
- java基础复习 常用修饰符 初始化顺序
- java并发编程汇总
- hdu 2037 今年暑假不AC (java)
- 企业支付宝账号开发接口教程--JAVA-UTF-8