Spring 是一款目前主流的 Java EE 轻量级开源框架 ,是 Java 世界最为成功的框架之一,在很多项目中,我们都会导入spring框架,那spring框架怎么导入?下面来我们就来给大家讲解一下。
1.首先下载Spring的jar包;
2.在web.xml文件里加入如下代码:
<span style="font-size: small;"> <!-- Spring Configures --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/*-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></span>
<span style="font-size: small;"><?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="exportTableCondition" class="com.kingstar.hbm.dorado.ExportTableCondition" /> <bean id="organEffectDetail" class="com.kingstar.hbm.dorado.OrganEffectDetail" /> <bean id="completedCaseDetail" class="com.kingstar.hbm.dorado.CompletedCaseDetail" /> <bean id="dealingClaimDetail" class="com.kingstar.hbm.dorado.DealingClaimDetail" /> <bean id="guaranteeClaimDetail" class="com.kingstar.hbm.dorado.GuaranteeClaimDetail" /> </beans> </span>
到此为止就可用spring了。
Spring如何实现文件上传?
分为前端JSP编写和后台Controller的编写。
前期准备工作,首先要引入commons-fileupload这个jar包,pom.xml中的配置如下:
<!-- 实现文件上传,spring集成了这个功能 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency>
然后配置拦截器,在dispatcher-servlet.xml中添加
<!-- 实现文件上传,这样一旦某个Request是一个MultipartRequest,它就会首先被MultipartResolver处理,然后再转发相应的Controller --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置上传文件的最大尺寸为1MB --> <property name="maxUploadSize"> <value>1048576</value> </property> </bean>
直接上代码:
1、前端JSP编写
upload.jsp
代码如下:
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script src="resources/js/jquery.js"></script> </head> <body> <form method="post" action="upload.do" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" /> </form> </body> </html>
2、后台UploadController编写
代码如下:
package nju.software.xkxt.web.controller; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; /** * @author typ * */ @Controller public class UploadController { /** * 进入下载界面 * * @param request * @param response * @param model * @return * @throws IOException */ @RequestMapping(value = "upload_enter.do", method = RequestMethod.GET) public String enter(HttpServletRequest request , HttpServletResponse response, ModelMap model) throws IOException { // 进入下载界面 return "upload"; } /** * 具体的实现下载功能 * * @param request * @param response * @param model * @throws IOException */ @RequestMapping(value = "upload.do", method = RequestMethod.POST) public void upload(HttpServletRequest request , HttpServletResponse response, ModelMap model) throws IOException { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; // 得到上传的文件 MultipartFile mFile = multipartRequest.getFile("file"); // 得到上传服务器的路径 String path = request.getSession() .getServletContext() .getRealPath("/WEB-INF/upload/"); // 得到上传的文件的文件名 String filename = mFile.getOriginalFilename(); InputStream inputStream = mFile.getInputStream(); byte[] b = new byte[1048576]; int length = inputStream.read(b); path += "\\" + filename; // 文件流写到服务器端 FileOutputStream outputStream = new FileOutputStream(path); outputStream.write(b, 0, length); inputStream.close(); outputStream.close(); } }
这样就很容易的实现了文件上传的功能,spring框架是企业项目中经常使用的框架之一,它能够简化 Java 企业级应用的开发难度和开发周期。提高开发人员的工作效率!最后大家如果想要了解更多java架构师知识,敬请关注奇Q工具网。
推荐阅读: