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

struts文件上传

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

struts文件上传

文件上传的三种方案 

文件上传的三种方案:

一:上传到tomcat服务器;

        自己的电脑:项目在哪,图片就在哪

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

二:上传到指定文件目录,添加服务器映射关系;

        文件服务器和Web服务器通常是同一个,但是文件目录与tomcat目录肯定不是同一个 

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

        安全性高 

实现文件上传

第一步:新建上传文件的页面

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




Insert title here





图片:

第二步:web层的搭建

package com.sg.one.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.sg.crud.dao.ClzDao;
import com.sg.crud.entity.Clz;
import com.sg.crud.util.baseAction;
import com.sg.crud.util.PageBean;

public class ClzAction extends baseAction {
	private Clz clz=new Clz();
	private ClzDao clzDao=new ClzDao();
	
	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;
	}
	
	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;
	}
	
	public String add() throws Exception{
		this.clzDao.add(clz);
		return TOLIST;
	}
	
	public String edit() throws Exception{
		this.clzDao.edit(clz);
		return TOLIST;
	}
	
	public String del() throws Exception{
		this.clzDao.del(clz);
		return TOLIST;
	}
	@Override
	public Clz getModel() {
		// TODO Auto-generated method stub
		return clz;
	}
	
    public String preUpload() throws Exception {
		this.result=this.clzDao.list(clz, null).get(0);
		this.req.setAttribute("result", result);
		return "upload";
	}
	
	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 upload() throws Exception{
//		img代表客户选择的文件或者图片,接下来要将图片上传到其他地方
//		img代表了源头,要将其写入目的地target
		String destDir="E:/temp/2021/mvc/upload";
		String serverDir="/uploadImages";
		FileUtils.copyFile(img, new File(destDir+"/"+imgFileName));
		
		clz.setPic(serverDir+"/"+imgFileName);
		this.clzDao.edit(clz);
		return TOLIST;
	}
}

第三步:配置xml

/upload.jsp

第四步:为主页面添加方法以及图片上传的按钮

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>	
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>	






博客列表

.page-item input {
	padding: 0;
	width: 40px;
	height: 100%;
	text-align: center;
	margin: 0 6px;
}

.page-item input, .page-item b {
	line-height: 38px;
	float: left;
	font-weight: 400;
}

.page-item.go-input {
	margin: 0 10px;
}



	
新增
ID 班级姓名 教员 图片 操作
${j.cid } ${j.cname } ${j.cteacher } 修改 删除 上传图片

结果展示:

拜拜,拜拜各位!!!

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

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

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