<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency>
<a href="testJson">测试Json</a>
@Controller public class TestJsonController { @ResponseBody @RequestMapping("/testJson") public Collection<String> testJson() { Collection<String> all = new ArrayList<>(); all.add("AAA"); all.add("BBB"); all.add("CCC"); return all; } }
public interface HttpInputMessage extends HttpMessage { /** * Return the body of the message as an input stream. * @return the input stream body (never {@code null}) * @throws IOException in case of I/O errors */ InputStream getBody() throws IOException; }
public interface HttpOutputMessage extends HttpMessage { /** * Return the body of the message as an output stream. * @return the output stream body (never {@code null}) * @throws IOException in case of I/O errors */ OutputStream getBody() throws IOException; }
<a href="down">下载图片</a>
@GetMapping("/down") public ResponseEntity<byte[]> down(HttpSession session) throws IOException { //获取下载文件的路径 //getRealPath()里什么也不填, 获取项目路径, 填写内容则获取项目下文件夹路径. String realPath = session.getServletContext().getRealPath("img"); String finalPath = realPath + File.separator + "1.jpg"; InputStream is = new FileInputStream(finalPath); //available()获取输入流所读取的文件的最大字节数 byte[] b = new byte[is.available()]; //把字节数字加载进输入流. is.read(b); //设置请求头 HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "attachment;filename=zzz.jpg"); //设置响应状态 HttpStatus statusCode = HttpStatus.OK; ResponseEntity<byte[]> entity = new ResponseEntity<>(b, headers, statusCode); return entity; }
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency>
<!-- id只能为multipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置文件解析的编码, 一定要和页面的pageEncoding一致 --> <property name="defaultEncoding" value="UTF-8"></property> <!-- 设置最大上传文件大小 --> <property name="maxUploadSize" value="88888"></property> </bean>
<form action="up" method="post" enctype="multipart/form-data"> 头像: <input type="file" name="uploadFile"> 描述: <input typee="text" name="desc"> <input type="submit" value="上传"> </form>
@PostMapping("up") public String up_new(String desc, MultipartFile uploadFile, HttpSession session) throws IOException { //获取上传文件名称 String filename = uploadFile.getOriginalFilename(); String finalFileName = UUID.randomUUID() + filename.substring(filename.lastIndexOf(".")); String path = session.getServletContext().getRealPath("photo") + File.separator + finalFileName; File file = new File(path); //把uploadFile中的内容转换到file中, 即上传. uploadFile.transferTo(file); return "success"; }
<mvc:interceptors> <!-- 方式一 --> <bean class="test.interceptor.FirstInterceptor"></bean> </mvc:interceptors>
<mvc:interceptors> <!-- 方式二: 必须在拦截器上加@Component注解 --> <ref bean="firstInterceptor"/> </mvc:interceptors>
<mvc:interceptors> <ref bean="firstInterceptor"/> <mvc:interceptor> <mvc:mapping path=""/> <!-- <mvc:exclude-mapping path=""/> --> </mvc:interceptor> </mvc:interceptors>
@Component public class FirstInterceptor implements HandlerInterceptor { /** * 返回true代表放行, 返回false代表拦截. */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("First: preHandle"); return false; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("First: postHandle"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("First: afterCompletion"); } }
原文:https://www.cnblogs.com/binwenhome/p/13051279.html