服务器推荐:ECS云服务器(阿里云、腾讯云、西部数码云)
二.使用 第一步:创建upload.jsp(定义多功能表单 enype="multiput/form-data")<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
文件上传界面
第二步:ClzAction
private File img;
private String imgFileName;
private String imgContentType;
public File getImg() {
return img;
}
public void setImg(File img) {
this.img = img;
}
public String getImgContentType() {
return imgContentType;
}
public void setImgContentType(String imgContentType) {
this.imgContentType = imgContentType;
}
public String getImgFileName() {
return imgFileName;
}
public void setImgFileName(String imgFileName) {
this.imgFileName = imgFileName;
}
public String preUpload() throws Exception {
this.result=this.cd.list(clz, null).get(0);
this.req.setAttribute("result", result);
return "upload";
}
public String upload() throws Exception {
//img代表客户选择的文件(图片),接下来要将图片上传到其他地方
//img代表了源头,要将其写入目的地target
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/1.png
//访问:http://localhost:8080/struts/uploadImages/xx.png
clz.setPic(serverDir+"/"+imgFileName);
this.cd.edit(clz);
return TOLIST;
}
第三步:配置struts-sy.xml
/upload.jsp
展示效果:



