在本服务器和其它服务器上传文件

springMVC框架本地服务器

1.页面:

<form action="fileUpload2" method="post" enctype="multipart/form-data">
    用户名:<input type="text" name="username"> <br>
    文件: <input type="file" name="imgFile"> <br>
    <input type="submit" value="上传">
</form>

2.控制器

package com.haifeng.file_upload;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * springMVC文件上传
 */
@Controller
public class FileUploadController2 {
    //springMVC框架为我们提供了一个对象MultipartFile,该对象可以作为控制器方法的参数
    //参数的名称和表单file元素的name属性必须一致
    @RequestMapping("fileUpload2")
    public String testFileUpload(String name, HttpServletRequest request, MultipartFile imgFile) throws IOException {
        System.out.println("username=" + name);
        //1.设置上传文件目录(发布路径)
        String basePath = request.getSession().getServletContext().getRealPath("/upload");
        //当前时间作为二级目录
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        //3 创建文件file对象,判断文件路径是否存在
        File file = new File(basePath,date);
        if (!file.exists()) {
            file.mkdirs();
        }
        //4 获取文件名
        String fileName = imgFile.getOriginalFilename();
        //5 为防止文件重名,最终文件名= uuid+fileName
        String uuidFileName = UUID.randomUUID().toString().replaceAll("_","").toUpperCase() + "_" + fileName;
        //6 上传文件
        imgFile.transferTo(new File(file,uuidFileName));
        return "success";
    }
}

 

springMVC.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.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.itheima"></context:component-scan>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

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

    <!-- 设置静态资源不过滤
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/images/" mapping="/images/**"/>
    -->
    <!--文件上传解析器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--最大10M-->
        <property name="maxUploadSize" value="10485760"></property>
    </bean>
</beans>

注意:
文件上传的解析器 id 是固定的,不能起别的名称,否则无法实现请求参数的绑定。(不光是文件,其他字段也将无法绑定)

springMVC框架其他服务器

1.文件服务器的部署

(1) 先解压一个tomcat服务器,这里用的是tomcat9

(什么版本都没有关系:如tomcat6、tomcat7、tomcat8、tomcat9

 

(2) 修改web.xml配置文件

如:

修改内容:

默认 readonly 为 true,当 readonly 设置为 false 时,可以通过一些方式进行文件操控。

 

(3) 创建一个文件系统,保存所有文件(文件上传到这里)

在tomcat的webapps目录下新建fileSystem文件系统

在fileSystem中新建upload目录、indexjsp测试文件即可

在conf/server.xml中修改tomcat端口

tomcat服务端口默认8080,修改为:6080

启动tomcat,访问index.jsp

这里只是测试下,

 

2导入 jersey 的坐标

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-core</artifactId>
    <version>1.18.1</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.18.1</version>
</dependency>

3.编写控制器实现上传图片

  1. @Controller
    public class FileUploadController3 {

        // 文件服务器地址
        private static final String FILESERVERURL = "http://localhost:6080/fileSystem/upload/";

        /**
         * springmvc框架为我们提供了一个对象MultipartFile,该对象可以作为控制器方法的参数。
         */
        @RequestMapping("fileUpload3")
        public String testFileUpload(MultipartFile imgFile) throws Exception {

            //1. 获取文件名
            String fileName = imgFile.getOriginalFilename();
            //2. 为了防止文件名重名,最终文件名=uuid + "_" + fileName
            String uuidFileName =
                    UUID.randomUUID().toString().replaceAll("-", "").toUpperCase() + "_" + fileName;
            //3.创建jersey包中提供的client对象
            Client client = new Client();
            //4.使用client和文件服务器建立联系
            WebResource resource = client.resource(FILESERVERURL + uuidFileName);
            //5.web资源对象写到文件服务器
            resource.put(String.class,imgFile.getBytes());
            return "success";
        }
    }

     

4.bean.xml配置文件上传解析器

<!--文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--最大10M-->
    <property name="maxUploadSize" value="10485760"></property>
</bean>

5.页面

<form action="fileUpload3" method="post" enctype="multipart/form-data">
    用户名:<input type="text" name="username"> <br>
    文件: <input type="file" name="imgFile"> <br>
    <input type="submit" value="上传">
</form>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值