转发与重定向及文件上传

在这里插入图片描述
SpringMVC转发与重定向
在一个请求处理方法Action中如果返回结果为“index”字符则表示转发到视图index,有时候我们需要重定向,则可以在返回的结果前加上一个前缀“redirect:”,可以重定向到一个指定的页面,也可以是另一个action,示例代码如下:

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("re")
public class ReController {
//转发
    @RequestMapping("test1")
    public String test1(Model model){
        System.out.println("hei");
        return "hi";
    }
//转发
    @RequestMapping("test2")
    public String test2(Model model){
        model.addAttribute("hai","hai");
        return "forward:test1";
    }

//重定向1
    @RequestMapping("test3")
    public String test3(Model model){
        model.addAttribute("hai3","hai3");
        return "redirect:test1";
    }
//重定向2
    @RequestMapping("test4")
    public String test4(Model model, RedirectAttributes ra){
        ra.addFlashAttribute("hai33","hai33");
        return "redirect:test1";
    }

}

在上述例子中,转发存在两种形式,如果转发方法则需要forward,重定向第一种不可带数据进行传递,重定向第二种则是使用Flash属性,是数据通过重定向实现传递
文件上传
配置
因为需要借助第三方上传组件commons-fileupload与commons-io,所以要修改pom.xml文件添加依赖,依赖的内容如下:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

springmvc-servlet.xml配置文件,增加如下配置内容:

 <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8" />
        <property name="maxUploadSize" value="10485760000" />
        <property name="maxInMemorySize" value="40960" />
    </bean>

增加了一个类型为CommonsMultipartResolver类型的解析器,各属性的意义:
defaultEncoding:默认编码格式
maxUploadSize:上传文件最大限制(字节byte)
maxInMemorySize:缓冲区大小
方法

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@Controller
@RequestMapping("up")
public class UpFileController {
    @RequestMapping(value="/fileSave")
    public String fileSave(Model model, @RequestParam MultipartFile[] files, HttpServletRequest request) throws Exception{
        String username=request.getParameter("username");
        System.out.println(username);
        //文件存放的位置
        String path=request.getServletContext().getRealPath("/files");
        String fileName=null;
        System.out.println(path);
        System.out.println(files.length);
        for (MultipartFile file : files) {
            fileName=file.getOriginalFilename();
            System.out.println(fileName);
            File tempFile=new File(path, fileName);
            System.out.println(tempFile.exists());
            if(!tempFile.exists()) {
                tempFile.mkdirs();
            }
            file.transferTo(tempFile);
        }
        System.out.println(path);
        model.addAttribute("path",path);
        model.addAttribute("fileName",fileName);
        return "upload";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值