SpringMVC文件上传(eclipse)

工程目录

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:p="http://www.springframework.org/schema/p"

       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-4.3.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

        ">

       <!-- 扫描注解包 -->

       <context:component-scan base-package="com.spring.controller"></context:component-scan>

       <!-- 允许访问静态资源 -->

       <mvc:resources location="/js/" mapping="/js/*"></mvc:resources>

       <!-- 创建适配器和映射器 -->

       <mvc:annotation-driven></mvc:annotation-driven>

     <!-- 4 逻辑视图,配置返回器的前后缀 -->

       <!-- 把modelAndView对象的逻辑视图解析为物理视图 前缀+逻辑视图+后缀= 物理视图 -->

       <bean

              class="org.springframework.web.servlet.view.InternalResourceViewResolver">

              <property name="suffix" value=".jsp"></property>

              <property name="prefix" value="/"></property>

       </bean>

      

       <!-- 文件上传解析器 -->

         <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>

      

</beans>

UploadController 控制器(两种写法)

package com.spring.controller;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

//标记成控制器

@Controller

public class UploadController {

       // 指定执行的方法,地址栏输入值

       @RequestMapping("/upload")

       public String upload(@RequestParam(value="file")CommonsMultipartFile file, HttpServletRequest request)

                     throws IOException {

              // 声明解析器,传入参数就是上传的文件

              // 以流的形式上传

              // 获得服务端文件上传到的地址

              String patch = request.getServletContext().getRealPath("/uploadimages");

              // 创建服务端文件上传的目录

              File dir = new File(patch);

              dir.mkdirs();

              // 获取文件上传文件的名称

              String filename = file.getOriginalFilename();

             

              // 以流的形式吧文件读取到java并写到目录

              byte fileb[] = new byte[20 * 1024];

              // 定义文件读取流获取上传的文件

              InputStream input = file.getInputStream();

              //声明服务端文件

              File outfile= new File(patch,filename);

              // 写入服务器磁盘文件

              FileOutputStream out = new FileOutputStream(outfile);

              // 实际读取到的字节

              int len = 0;

              while ((len = input.read(fileb)) != -1) {// 把文件读到数组总

                     out.write(fileb, 0, len);

              }

              System.out.println("上传文件成功!服务端路径是:\t"+outfile.getAbsolutePath());

             

              // 关闭流

              out.close();

              input.close();

              return "main";

       }

       //方法2:标记成控制器

       @RequestMapping("/upload1")

       public String upload1(@RequestParam(value="file") MultipartFile file,HttpServletRequest request) throws IOException{//参数就是上传的文件对象

              //得到服务端保存图片的地址

              String path = request.getServletContext().getRealPath("/upload");

              File dir = new File(path);

              dir.mkdir();

             

              //得到上传的文件名称

              String fileNamefile.getOriginalFilename();

             

              //把客户端图片保存到服务端

              file.transferTo(new File(path,fileName));

              System.out.println("上传文件成功!");

              return "main";

       }

       }

JSP页面2个

<%@ 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>文件上传</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">

       -->

  </head>

 

  <body>

        <!--

              文件上传:

                method:post

                enctype:multipart/form-data

                type="file"

               -->

               <form action="${pageContext.request.contextPath }/upload" enctype="multipart/form-data" method="post">

                   <input type="file" name="file">

                   <input type="submit" value="上传">

               </form>

      

  </body>

</html>

<%@ 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>上传文件成功!</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">

       -->

  </head>

 

  <body>

   文件上传成功

  </body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值