文件上传(MultipartFile)
Posted lsk-130602
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件上传(MultipartFile)相关的知识,希望对你有一定的参考价值。
采用springmvc框架实现上传
jar包支持
springMVC配置文件
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 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-4.0.xsd"> <context:component-scan base-package="com.atguigu"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 静态资源需要默认的servlet来处理 --> <mvc:default-servlet-handler/> <mvc:annotation-driven></mvc:annotation-driven> <!-- id为固定的,只能为multipartResolver 作用:将客户端上传的file对象转换成MultipartFile对象, 设置文件解析的编码一定要与页面编码保持一致pageEncoding xml的属性是纯文本,不能写运算符,属性 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"></property> <property name="maxUploadSize" value="888888888"></property> </bean> </beans>
1. file.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>测试上传与下载</title> </head> <body> <a href="down">down</a> <form action="up" method="post" enctype="multipart/form-data"> 头像 :<input type="file" name="uploadFile"> 描述 :<input type="text" name="desc"> <input type="submit" value="上传"> </form> </body> </html>
2.UploadController
package com.atguigu.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; import javax.servlet.http.HttpSession; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; @Controller public class UploadAndDown { @RequestMapping("/down") public ResponseEntity<byte[]> down(HttpSession session) throws Exception{ //获取下载文件的路径 (获取tomcat服务器的位置) String realPath = session.getServletContext().getRealPath("img"); String finalPath = realPath+ File.separator +"1.jpg"; //创建字节输入流 InputStream in = new FileInputStream(finalPath); //available():获取输入流所读取的文件的最大字节数 byte[] body = new byte[in.available()]; //把字节读取到数组中 in.read(body); //设置请求头 MultiValueMap<String, String> headers = new HttpHeaders(); headers.add("Content-Disposition", "attachment;filename=aaa.jpg"); //设置响应状态 HttpStatus statusCode = HttpStatus.OK; in.close(); ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode); return entity; } @RequestMapping(value="/up_old", method = RequestMethod.POST) public String up_old (String desc,MultipartFile uploadFile,HttpSession session) throws Exception { //获取上传文件的名称 String fileName = uploadFile.getOriginalFilename(); //获取上传文件的路径 String path = session.getServletContext().getRealPath("photo")+File.separator+fileName; //获取输入流 InputStream is = uploadFile.getInputStream(); //获取输出流 OutputStream os = new FileOutputStream(new File(path)); //开始复制 int i = 0; byte[] bytes = new byte[1024]; while((i = is.read(bytes))!=-1) { os.write(bytes, 0, i); } os.close(); is.close(); return "success"; }
/**
写法二
* 使用springmvc实现上传,采用MultipartFile的transfer() * @param desc * @param uploadFile * @param session * @return * @throws Exception */ @RequestMapping(value="/up", method = RequestMethod.POST) public String up (String desc,MultipartFile uploadFile,HttpSession session) throws Exception { //获取上传文件的名称 String fileName = uploadFile.getOriginalFilename(); //解决文件重名问题 String finalFileName = UUID.randomUUID().toString()+fileName.substring(fileName.lastIndexOf(".")); //获取上传文件的路径 String path = session.getServletContext().getRealPath("photo")+File.separator+finalFileName; uploadFile.transferTo(new File(path)); return "success"; } }
以上是关于文件上传(MultipartFile)的主要内容,如果未能解决你的问题,请参考以下文章
springboot使用MultipartFile上传文件以及File与MultipartFile互转
springboot使用MultipartFile上传文件以及File与MultipartFile互转