jquery上传图片_Spring实现图片的上传和下载

d4fdc06a6cb17f4bda3453a59fea4341.png

spring是一个很强大的框架,使用它可以完成几乎所有的功能,对于图片的上传与下载也挺方便的。要实现文件的上传,必须在SpringMVC框架的核心配置文件中注册文件上传解析器,并且,为了解决中文名字的问题,要将文件解析器中的defaultEncoding属性设置为utf-8.

首先编写简单的jsp页面:对于上传的表单我们要在表单属性中加入属性: enctype="multipart/form-data"

%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.js"></script>
</head>
  
  <body>
		 <form action="${pageContext.request.contextPath }/springmvc/fileUpload" method="POST"  enctype="multipart/form-data">
			<input type="file" name="imgs">
			<input type="file" name="imgs">
			<input type="file" name="imgs">
			<input type="submit" value="提交">
		</form>
		<a href="${pageContext.request.contextPath }/springmvc/fileDownload">下载</a>
	
	
  </body>
</html>

对于控制器这样写:

/**

接下来就是springMVC的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 注册开发中要进行注册组件扫描器 -->
	<context:component-scan base-package="com.lianxi.handlers"></context:component-scan>
	<!-- 注解开发第二步,注册注解驱动 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 注册视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 视图层前缀 -->
		<property name="prefix" value="/jsp/"></property>
		<!-- 视图层后缀-->
		<property name="suffix" value=".jsp"></property>
	</bean>
	<!-- 注册文件上传解析器 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8"></property>
	</bean>
	<!-- 解决静态资源第二种方式 -->
	<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
	<!-- 解决静态资源第二种方式 -->
	<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
	
</beans> 

web.xml文件的配置就不列出来了,这样就可以实现文件的简单的上传与下载的功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值