文件上传的三种方案
文件上传的三种方案:
一:上传到tomcat服务器;
自己的电脑:项目在哪,图片就在哪
云服务器:是没有CDEF盘,只有根目录
二:上传到指定文件目录,添加服务器映射关系;
文件服务器和Web服务器通常是同一个,但是文件目录与tomcat目录肯定不是同一个
三:在数据库表中建立二进制字段,将图片存储到数据库;
安全性高
实现文件上传
第一步:新建上传文件的页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/sy/clz_upload.action" method="post" enctype="multipart/form-data">
<input type="hidden" name="cid" value="${result.cid }"><br>
<input type="hidden" name="cname" value="${result.cname }"><br>
<input type="hidden" name="cteacher" value="${result.cteacher }"><br>
<input type="file" name="img"><br>
<input type="submit">
</form>
</body>
</html>
第二步:web层的搭建
package com.lj.web;
import java.io.File;
import org.apache.commons.io.FileUtils;
import com.lj.dao.ClzDao;
import com.lj.entity.Clz;
import com.zking.util.BaseAction;
import com.zking.util.PageBean;
public class ClzAction extends BaseAction<Clz>{
private Clz clz=new Clz();
private ClzDao clzDao=new ClzDao();
/**
* 查询班级列表
* @return
* @throws Exception
*/
public String list() throws Exception {
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
this.result=this.clzDao.list(clz, pageBean);
this.req.setAttribute("result", result);
this.req.setAttribute("pageBean", pageBean);
return LIST;
}
/**
* 跳转新增/修改界面
* @return
* @throws Exception
*/
public String toEdit() throws Exception {
int cid = clz.getCid();
if(cid!=0) {
this.result=this.clzDao.list(clz,null).get(0);
this.req.setAttribute("result", result);
}
return TOEDIT;
}
/**
* 往数据库中新增数据
* @return
* @throws Exception
*/
public String add() throws Exception {
this.clzDao.add(clz);
return TOLIST;
}
/**
* 删除数据
* @return
* @throws Exception
*/
public String del() throws Exception {
this.clzDao.del(clz);
return TOLIST;
}
/**
* 修改数据
* @return
* @throws Exception
*/
public String edit() throws Exception {
this.clzDao.edit(clz);
return TOLIST;
}
@Override
public Clz getModel() {
// TODO Auto-generated method stub
return clz;
}
/**
* 按照指定的格式去接受参数变量
* 1.上传的文件
* 2.上传的文件名
* 3.上传文件的类别
*/
private File img;
private String imgFileName;
private String imgContentType;
public Clz getClz() {
return clz;
}
public void setClz(Clz clz) {
this.clz = clz;
}
public ClzDao getClzDao() {
return clzDao;
}
public void setClzDao(ClzDao clzDao) {
this.clzDao = clzDao;
}
public File getImg() {
return img;
}
public void setImg(File img) {
this.img = img;
}
public String getImgFileName() {
return imgFileName;
}
public void setImgFileName(String imgFileName) {
this.imgFileName = imgFileName;
}
public String getImgContentType() {
return imgContentType;
}
public void setImgContentType(String imgContentType) {
this.imgContentType = imgContentType;
}
/**
* 跳转到文件上传界面
*/
public String preUpload() throws Exception {
this.result = this.clzDao.list(clz, null).get(0);
this.req.setAttribute("result", result);
return "upload";
}
/**
* 上传图片的方法
*/
public String upload() throws Exception {
// img代表客户选择的文件或图片,接下来要将图片上传到其他地方
String destDir="E:/temp/2021/mvc/upload";
String serverDir="/uploadImages";
// 源文件考到目的地
FileUtils.copyFile(img, new File(destDir+"/"+imgFileName));
//将图片加到数据库
//数据库保存的值是:/uploadImages/xx.png
//图片是在:E:/temp/2021/mvc/upload/
//访问:http://localhost:8080/my_struts2/uploadImages/20210925102437.png
clz.setPic(serverDir+"/"+imgFileName);
this.clzDao.edit(clz);
return TOLIST;
}
}
第三步:配置xml
第四步:为主页面添加方法以及图片上传的按钮
结果展示: