基于JSP下的文件上传下载

1.导入所依赖的架包

2.在web.xml下面配置DispatcherServlet,中文乱码过滤器

3.配置spring-mvc.xml


		
		
		
		
		
		
		
		
		
		
		
		10485760
		
		 
		  
            UTF-8
        
	

4.编写一个简单的login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




用户注册


    

用户注册

用户名:
请上传头像:

5.创建一个user实体类,用来接收前端的文件描述和图片

package com.zhiyou100.entity;

import java.io.Serializable;

import org.springframework.web.multipart.MultipartFile;

public class User implements Serializable{
	private String ename;
	private MultipartFile image;
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public MultipartFile getImage() {
		return image;
	}
	public void setImage(MultipartFile image) {
		this.image = image;
	}
	
}

6.需要对请求进行处理

UserUploadController.java

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhiyou100.entity.User;

@Controller
public class UserUploadController {
	@RequestMapping("/uploadUser.do")
	public String uploadUser(HttpServletRequest request,
			Model model,User user) throws Exception {
		//如果文件不为空,写入上传路径
		if(!user.getImage().isEmpty()) {
			//上传文件路径
			//String path = "最好写到你的工程路径下面,在webapp下面创建一个images文件夹";
			String path = request.getServletContext().getRealPath("/images/");
			//上传文件名
			String filename = user.getImage().getOriginalFilename();
            //保存用于回显图片
			request.setAttribute("image", filename);
			File filepath = new File(path, filename);
			//判断路径是否存在,不存在就创建一个
			if(!filepath.getParentFile().exists()) {
				filepath.getParentFile().mkdirs();
			}
			//将上传文件保存到一个文档目录里
			user.getImage().transferTo(new File(path + File.separator+ filename));
			 //将用户添加到model
            model.addAttribute("user", user);
			return "userInfo";
		}else {
			return "error";
		}
		
		
	}
	
}

7.创建回显页面userInfo.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




文件下载


文件下载

${requestScope.user.image.originalFilename }

8.对下载请求做出响应

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class DownController {
	@RequestMapping(value="/download.do")
    public ResponseEntity download(HttpServletRequest request,
            @RequestParam("filename") String filename,
            Model model)throws Exception {
       //下载文件路径
       String path = request.getServletContext().getRealPath("/images/");
       File file = new File(path + File.separator + filename);
       HttpHeaders headers = new HttpHeaders();  
       //下载显示的文件名,解决中文名称乱码问题  
       String downloadFielName = new String(filename.getBytes("UTF-8"),"iso-8859-1");
       //通知浏览器以attachment(下载方式)打开图片
       headers.setContentDispositionFormData("attachment", downloadFielName); 
       //application/octetam : 二进制流数据(最常见的文件下载)。
       headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
       return new ResponseEntity(FileUtils.readFileToByteArray(file),    
               headers, HttpStatus.CREATED);  
 
    }
}

9.效果

基于JSP下的文件上传下载_第1张图片

基于JSP下的文件上传下载_第2张图片

基于JSP下的文件上传下载_第3张图片

基于JSP下的文件上传下载_第4张图片

 

你可能感兴趣的:(新手必看,编程规则,java,小东升职记)