1.文件上传,文件下载
文件上传
1.spring-xml配置多功能视图解析器
2.前端标记表单为多功能表单enctype=”mutipart/form-data“
3.后端可以直接利用mutipartFile类,接受前端传递到后台的文件
4.将文件转成流,然后写到服务器(某一个硬盘)
5.做硬盘于网络地址的映射(服务器配置)
package com.zlj.web;
import com.zlj.biz.StuBiz;
import com.zlj.model.Stu;
import com.zlj.util.PageBean;
import com.zlj.util.PropertiesUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.PathVariable;
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.util.List;
/**
* @author zlj
* @create 2023-09-08 16:55
*/
@Controller
@RequestMapping("stu")
public class StuController {
@Autowired
private StuBiz stuBiz;
// 增
@RequestMapping("/add")
public String add(Stu stu,HttpServletRequest request) {
int i = stuBiz.insert(stu);
return "redirect:list";
}
// 删
@RequestMapping("/del/{sid}")
public String del(@PathVariable("sid") Integer sid) {
stuBiz.deleteByPrimaryKey(sid);
return "redirect:/stu/list";
}
// 改
@RequestMapping("/edit")
public String edit(Stu stu) {
stuBiz.updateByPrimaryKeySelective(stu);
return "redirect:list";
}
//文件上传
@RequestMapping("/upload")
public String upload(Stu stu,MultipartFile xxx){
try {
// 3.后端可以直接利用mutipartFile类,接受前端传递到后台的文件
// 4.将文件转成流,然后写到服务器(某一个硬盘)
// 上传的图片真实的地址
String dir= PropertiesUtil.getValue("dir");
// 网络访问的地址
String server=PropertiesUtil.getValue("server");;
//文件名
String filename=xxx.getOriginalFilename();
System.out.println("文件名"+filename);
System.out.println("文件类别"+xxx.getContentType());
FileUtils.copyInputStreamToFile(xxx.getInputStream(),new File(dir+filename));
// /upload/0703.png
stu.setSpic(server+filename);
stuBiz.updateByPrimaryKeySelective(stu);
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:list";
}
// 查
@RequestMapping("/list")
public String list(Stu stu, HttpServletRequest request) {
//stu是用来接收前台传递后台的参数
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
List stus = stuBiz.ListPager(stu, pageBean);
request.setAttribute("lst", stus);
request.setAttribute("pageBean", pageBean);
// WEB-INF/jsp/stu/list.jsp
return "stu/list";
}
@RequestMapping(value="/download")
public ResponseEntity download(Stu stu,HttpServletRequest req){
try {
//先根据文件id查询对应图片信息
Stu stus = this.stuBiz.selectByPrimaryKey(stu.getSid());
String diskPath = PropertiesUtil.getValue("dir");
String reqPath = PropertiesUtil.getValue("server");
// /upload/0703.png -->D:/temp/upload/0703.png
String realPath = stus.getSpic().replace(reqPath,diskPath);
String fileName = realPath.substring(realPath.lastIndexOf("/")+1);
//下载关键代码
File file=new File(realPath);
HttpHeaders headers = new HttpHeaders();//http头信息
String downloadFileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");//设置编码
headers.setContentDispositionFormData("attachment", downloadFileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//MediaType:互联网媒介类型 contentType:具体请求中的媒体类型信息
return new ResponseEntity(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
//数据回显
@RequestMapping("/preSave")
public String preSave(Stu stu, Model model) {
if (stu != null && stu.getSid() != null && stu.getSid() != 0) {
Stu s = stuBiz.selectByPrimaryKey(stu.getSid());
model.addAttribute("s", s);
}
return "stu/edit";
}
}
//list.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@include file="/common/header.jsp"%>
学生信息
学生id
学生姓名
学生年龄
学生图片
操作
${s.sid }
${s.same }
${s.sage }
修改
删除
图片上传
图片下载
//upload.jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@include file="/common/header.jsp"%>
学生log上传
2.jrebel&多文件上传
jrebel使用
1下载插件。
2.下载后,打开IDEA,选择File—>Settings—>Plugins—>设置按钮—>Installed Plugin from Disk(从文件夹选择已下载的插件安装)。3.重启IDEA
4.选择File—>Settings—>JRebel & XRebel—>Change license5.安装JRebel插件后,注册地址填写网址 + 生成的GUID,邮箱随便填写,然后即可。
http://jrebel-license.jiweichengzhu.com/{GUID}
https://jrebel.qekang.com/{GUID}
GUID可以使用在线GUID在线生成在线生成,然后替换{GUID}就行。
6.下面邮箱地址可随便输入。
7.选择我同意
8.提交
多文件上传
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@include file="/common/header.jsp"%>
学生log上传
package com.zlj.web;
import com.zlj.biz.StuBiz;
import com.zlj.model.Stu;
import com.zlj.util.PageBean;
import com.zlj.util.PropertiesUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.PathVariable;
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.util.List;
/**
* @author zlj
* @create 2023-09-08 16:55
*/
@Controller
@RequestMapping("stu")
public class StuController {
@Autowired
private StuBiz stuBiz;
// 增
@RequestMapping("/add")
public String add(Stu stu,HttpServletRequest request) {
int i = stuBiz.insert(stu);
return "redirect:list";
}
// 删
@RequestMapping("/del/{sid}")
public String del(@PathVariable("sid") Integer sid) {
stuBiz.deleteByPrimaryKey(sid);
return "redirect:/stu/list";
}
// 改
@RequestMapping("/edit")
public String edit(Stu stu) {
stuBiz.updateByPrimaryKeySelective(stu);
return "redirect:list";
}
//文件上传
@RequestMapping("/upload")
public String upload(Stu stu,MultipartFile xxx){
try {
// 3.后端可以直接利用mutipartFile类,接受前端传递到后台的文件
// 4.将文件转成流,然后写到服务器(某一个硬盘)
// 上传的图片真实的地址
String dir= PropertiesUtil.getValue("dir");
// 网络访问的地址
String server=PropertiesUtil.getValue("server");;
//文件名
String filename=xxx.getOriginalFilename();
System.out.println("文件名"+filename);
System.out.println("文件类别"+xxx.getContentType());
FileUtils.copyInputStreamToFile(xxx.getInputStream(),new File(dir+filename));
// /upload/0703.png
stu.setSpic(server+filename);
stuBiz.updateByPrimaryKeySelective(stu);
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:list";
}
// 查
@RequestMapping("/list")
public String list(Stu stu, HttpServletRequest request) {
//stu是用来接收前台传递后台的参数
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
List stus = stuBiz.ListPager(stu, pageBean);
request.setAttribute("lst", stus);
request.setAttribute("pageBean", pageBean);
// WEB-INF/jsp/stu/list.jsp
return "stu/list";
}
//下载文件
@RequestMapping(value="/download")
public ResponseEntity download(Stu stu,HttpServletRequest req){
try {
//先根据文件id查询对应图片信息
Stu stus = this.stuBiz.selectByPrimaryKey(stu.getSid());
String diskPath = PropertiesUtil.getValue("dir");
String reqPath = PropertiesUtil.getValue("server");
// /upload/0703.png -->D:/temp/upload/0703.png
String realPath = stus.getSpic().replace(reqPath,diskPath);
String fileName = realPath.substring(realPath.lastIndexOf("/")+1);
//下载关键代码
File file=new File(realPath);
HttpHeaders headers = new HttpHeaders();//http头信息
String downloadFileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");//设置编码
headers.setContentDispositionFormData("attachment", downloadFileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//MediaType:互联网媒介类型 contentType:具体请求中的媒体类型信息
return new ResponseEntity(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
//多文件上传
@RequestMapping("/uploads")
public String uploads(HttpServletRequest req, Stu stu, MultipartFile[] files){
try {
StringBuffer sb = new StringBuffer();
for (MultipartFile file : files) {
//思路:
//1) 将上传图片保存到服务器中的指定位置
String dir = PropertiesUtil.getValue("dir");
String server = PropertiesUtil.getValue("server");
String filename = file.getOriginalFilename();
FileUtils.copyInputStreamToFile(file.getInputStream(),new File(dir+filename));
sb.append(filename).append(",");
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:list";
}
//数据回显
@RequestMapping("/preSave")
public String preSave(Stu stu, Model model) {
if (stu != null && stu.getSid() != null && stu.getSid() != 0) {
Stu s = stuBiz.selectByPrimaryKey(stu.getSid());
model.addAttribute("s", s);
}
return "stu/edit";
}
}