springmvc上传图片至指定路径

index.html

<body>
	<!-- enctype="multipart/form-data"告诉浏览器以二进制流的形式发送 -->
	<form action="file/upload.do" method="post" enctype="multipart/form-data">
		<input type="text" name="desc" placeholder="请输入描述信息" /><br>
		<input type="file" name="myFile" placeholder="请选择文件上传" /><br>
		<input type="submit" value="上传" />
	</form>
</body>

success.jsp

<body>
成功页面
</body>

FileUploadController

package cn.ssyszbd.controller;
import java.io.File;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping("/file")
public class FileUploadController {
	@RequestMapping(value = "upload", method=RequestMethod.POST)
	public String uploadFile(MultipartFile myFile, String desc, HttpServletRequest req) {
		System.out.println(desc);
		
		ServletContext application = req.getServletContext();
		String basePath = application.getRealPath("/upload");//这个路径很深,而且不是当前项目下路径
//		basePath  = "/usr/local/upload";
		basePath = "F:\\";//手动指定路径
		System.err.println("basePath = " + basePath);
		String fileName = myFile.getOriginalFilename();
		File dest = new File(basePath + File.separator + fileName);
		
		try {
			myFile.transferTo(dest);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	
}

springmvc.xml

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<!-- 扫描加了Controller注解的类 -->
	<!-- @Component @Repository @Controller @Service -->
	<context:component-scan base-package="cn.ssyszbd.controller" />
	<mvc:annotation-driven />
	<!-- 配置文件上传的解析器 -->
	<!-- 使用apache下的commons-fileupload.jar commons-io.jar -->
	<!-- 拷贝两个jar包 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 设置最大上传大小单位是字节 -->
<!-- 		<property name="maxUploadSize"></property> -->
		<!-- 设置上传文件单个大小,单位是字节,默认-1,表示不限制 -->
<!-- 		<property name="maxUploadSizePerFile"></property> -->
		<!-- 在内存中设置多大空间暂存上传的文件,如果文件超过了这个值,则会将暂存的内容写到磁盘的临时位置,文件接着接收 -->
		<!-- 单位是字节默认10240字节,10kB -->
<!-- 		<property name="maxInMemorySize"></property> -->
		<!-- 设置表单的编码格式,包括报文头中的汉字等,包括表单中的普通栏位等,都是用这个编码方式 -->
		<!-- 默认的是ISO-8859-1 -->
		<property name="defaultEncoding" value="utf-8"></property>
		<!-- 设置临时文件存放的位置 -->
		<!-- 比如:内存中允许10kB,上传文件为10M,则一边接收文件到内存,一边将内存中的数据写入到这个临时位置 -->
		<!-- 默认存放在servlet container's temporary directory for the web application. -->
		<!-- tomcat为该应用指定的临时空间 -->
<!-- 		<property name="uploadTempDir"></property> -->
	</bean>
	<!-- 视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView"></property>
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

web.xml

 <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

项目源代码

https://download.csdn.net/download/qq_35505699/19786186

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值