文件上传
1.添加jar包,maven中dependency>
commons-fileupload
commons-fileupload
1.3.3
2.在spring 文件中,设置上传文件的配置信息bean id="multipartResolver"
p:defaultEncoding="UTF-8" p:maxUploadSize="10485760" class="org.spring work.web.multipart.commons.CommonsMultipartResolver"
切记 id一定要写成上面的名字
public void upd(MultipartFile file1, String username, HttpServletRequest request) throws IOException { byte b[] = file1.getBytes(); // 获得文件的运行路径,及文件的下载名称 File file = new File(request.getServletContext().getRealPath("WEB-INF/upload"), file1.getOriginalFilename()); FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write(b); fileOutputStream.close(); }下载代码
public void xia(MultipartFile file1, HttpServletResponse response) throws IOException { //说明文件路径及文件类型 及上传的编码格式 File file = new File("F:一些软件压缩包Keymaker-CORE(思维导图).rar"); response.setContentType("application/x-rar-compressed"); response.setCharacterEncoding("UTF-8"); //得到父路径名称,进行替换的到子类的文件名,并说明子类的文件名格式 String name = file.getCanonicalPath(); name = name.replace(file.getParent() + "", ""); String filename = new String(name.getBytes(), "ISO-8859-1"); //将文件名称带入到头信息中,并说明是下载 response.setHeader("Content-Disposition", "attachment;filename=" + filename); //从response中拿到outputStrem流 //用FileInputStream从文件上读上来 用outputStrem通过tcp进行传输 FileInputStream fileInputStream = new FileInputStream(file); byte by[] = new byte[8 * 1024]; int leng; OutputStream outputStream = response.getOutputStream(); while ((leng = fileInputStream.read(by)) != -1) { outputStream.write(by, 0, leng); outputStream.flush(); } fileInputStream.close(); }defaultServlet
因为mvc中静态资源进行了拦截所以要在配置文件中配置!--优先级低 所有的类都没找见他再去找-->
mvc:default-servlet-handler>
mvc:resources 资源配置
因为我们有时候需要将静态资源放在WEB-INF下,当我们配置,外界就可以访问我们指定的 WEB-INF指定的夹子(好处打包可以将静态资源打包进去)
mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
//mapping 网络访问路径
//location 本地文件夹路径
拦截器就是对你的访问进行拦截(可以进行合法型判断,登录做记录,日志等操作),也可以在返回参数的时候补参数
拦截器开发
1.写一个普通类型实现接口 HandlerInterceptor 2.里面有三个方法重写 preHandle(action业务方法请求之前进行访问) 参数设置 true 继续向下走 false 终止 下面两个给力请求头和响应头 可以进行补参 等等操作 postHandle(action中的业务方法执行完毕后进行试图渲染前的调用) afterCompletion(视图渲染后的调用) 3.配置文件信息 mvc:interceptors>mvc:interceptor>
<mvc:mapping path="/admin/**"/>//网络访问路径 <mvc:exclude-mapping path="/admin/test2"/> 受到管理的对象 <bean class="com.kaige123.view.TestHandlerInterceptor"/></mvc:interceptor> 继续阅读与本文标签相同的文章
上一篇 :
阿里云双十一服务器拼团优惠解析不会玩的看这里
-
支付宝王益:40岁写30年代码是一种什么体验? | 10月21号栖夜读
2026-05-17栏目: 教程
-
阿里云日志服务构建网站实时分析大盘实战
2026-05-17栏目: 教程
-
说到做到!平头哥开源MCU设计平台,加速生态建设
2026-05-17栏目: 教程
-
阿里云Kubernetes CSI实践—NAS动态存储卷使用
2026-05-17栏目: 教程
-
阿里巴巴成立政务钉钉事业部 已服务123万公务人员
2026-05-17栏目: 教程
