栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

struts文件上传

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

struts文件上传

文件上传的三种方案

1.上传到tomcat服务器

2.上传到指定文件目录,添加服务器与真实目录的映射关系

3.在数据库表中建立二进制字段,将图片存储到数据库(安全性比第二种高)

注意:

①上传文件界面:enctype="multipart/form-data"    type="file"

②struts必须按照指定的格式去接收参数变量

1、upload.jsp(定义多功能表单)

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




Insert title here





图片

2.ClzAction 

   先调用list方法查询到单条数据

   然后调用修改的方法对图片路径进行修改

	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代表客户选择的文件或者图片,接下来要将图片上传到其他地方
		//img代表源头。要将其写入目的地target
		String destDir="E:/temp/2021/mvc/upload";
		String serverDir="/uploadImages";
		FileUtils.copyFile(img, new File(destDir+"/"+imgFileName));
		//数据库保存的值是/upload/xx.png
		//图片是在E:/temp/2021/mvc/upload/1.png
		//访问:http://localhost:8080/struts/uploadImages/xx.png
		clz.setPic(serverDir+"/"+imgFileName);

		this.clzDao.edit(clz);
		return TOLIST;
	}
	
	

3、struts-sy.xml配置

	/upload.jsp

4.映射路径(server.xml)

     	

 5.clzlist.jsp修改代码

6.运行结果

①点击上传图片

 ②提交成功

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/337433.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号