-
- 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());
}