springboot restful api 接口
package com.example.demo;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/reg")
public class Example {
//接收url中的参数
@RequestMapping("/token/{id}")
String test1(@PathVariable(name = "id") String id) throws InterruptedException{
this.task1();
// return id+",接收路径中的id!";
return "id";
}
//get获取url的?后的参数
@RequestMapping("/token")
String test12() throws InterruptedException{
this.task1();
String token = "qwer1234";
System.out.println("返回token是" +token);
return token;
}
@Async
public void task1() throws InterruptedException {
long currentTimeMillis = System.currentTimeMillis();
Thread.sleep(3000);
long currentTimeMillis1 = System.currentTimeMillis();
System.out.println("task1任务耗时:" + (currentTimeMillis1 - currentTimeMillis) + "ms");
}
//get获取url的?后的参数,参数非必传
@RequestMapping("/message")
String test2(@RequestParam(name = "token", required = false) String token) {
System.out.println("接收到token是"+token);
return "接收到token是"+token;
}
//post获取上传参数
@PostMapping(path = "/token")
public String test3(@RequestBody Map<String, String> person) {
// 参数是json
return person.get("name").toString();
}
//文件上传
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public String handleFileUpload(HttpServletRequest request) {
MultipartHttpServletRequest params=((MultipartHttpServletRequest) request);
List<MultipartFile> files = ((MultipartHttpServletRequest) request)
.getFiles("file");
String name=params.getParameter("name");
System.out.println("name:"+name);
String id=params.getParameter("id");
System.out.println("id:"+id);
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i = 0; i < files.size(); ++i) {
file = files.get(i);
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
// stream = new BufferedOutputStream(new FileOutputStream(
// new File("/Users/xubh/Downloads/files/"+file.getOriginalFilename())));
stream = new BufferedOutputStream(new FileOutputStream(
new File("/Users/xubh/Downloads/files/"+file.getName())));
stream.write(bytes);
stream.close();
} catch (Exception e) {
stream = null;
return "You failed to upload " + i + " => "
+ e.getMessage();
}
} else {
return "You failed to upload " + i
+ " because the file was empty.";
}
}
return "upload successful";
}
//文件下载
@RequestMapping(value = "/export")
public ResponseEntity<FileSystemResource> listExport(String proNo) {
File file = new File("/Users/xubh/Downloads/files/file");
if (file == null) {
return null;
}
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" + System.currentTimeMillis() + ".xls");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
return ResponseEntity
.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new FileSystemResource(file));
}
}