栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Struts之文件上传

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

Struts之文件上传

一、文件上传的三种方式 1、上传到tomcat服务器 ;

      ①自己的电脑,项目在哪里,图片就在哪里;

      ②云服务器:是没有CDEF盘的,只有/根目录

 2上传到指定文件目录(用的多)

添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系文件服务器和web服务器通常是一个,但是文件目录与Tomcat目录肯定不是同一个

3、在数据库表中建立二进制字段,将图片存储到数据库;

      安全性比第二种高

服务器:ECS云服务器(阿里云、腾讯云、西部数码云)

注意事项:

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

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

二、文件上传代码(第二种) 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 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));
    	//将图片加到数据库
    	//数据库保存的值是:/uploadImages/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;
	}	
struts-sy.xml配置


    
           
      /clz_list
      /clzList.jsp
      /clzEdit.jsp
      /upload.jsp
      
    

server.xml

本地文件路径

   

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




Insert title here





图片:
clzlist.jsp

新增一行文件上传

展示效果:

 

 

-------------------没有了---------------------------------

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

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

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