关于前端文件上传后将文件保存至服务器路径存储在数据库并在相应页面展示的总结

前期准备

1.开发环境及框架的搭建。(基于SSH开发框架)
2.数据库建表,表应该有一个字段用来存储文件在服务区上的存储路径。
3.map.xml文件
4.Action.xml文件
5.写好实体类及get()set()方法
6.DAO层
7.Action()实现,Action()里面需要实现文件的上传服务器功能和路径存储数据库功能。
8.前端页面,前端页面需要实现文件的选择功能。

具体实例展示

1.数据库建表

关于前端文件上传后将文件保存至服务器路径存储在数据库并在相应页面展示的总结_第1张图片
其中image字段用来存储文件在服务器上的存储路径。
2.增删查改(map.xml文件)




	
	
	
	
		
		
		
		
	
	
		
	


	
	
	
	

3.dao层

package com.qkj.finishwinewl.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.iweb.sys.AbstractDAO;

/**
 * @author xiaoyan
 *
 */
public class finishwinewlDao extends AbstractDAO{

    public List list(Map map) {
		return super.list("finishwinewl_getfinishwinewl", map);
	}
	
	public Object get(Object uuid) {
		Map map = new HashMap();
		map.put("uuid", uuid);
		return super.get("finishwinewl_getfinishwinewl", map);
	}
	
	public Object add(Object parameters) {
		return super.add("finishwinewl_addfinishwinewl", parameters);
	}

	public int save(Object parameters) {
		return super.save("finishwinewl_mdyfinishwinewl", parameters);
	}

	public int delete(Object parameters) {
		return super.delete("finishwinewl_delfinishwinewl", parameters);
	}

	public int getResultCount() {
		return super.getResultCount();
	}
	
}

4.Action实现

package com.qkj.finishwinewl.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.iweb.sys.ContextHelper;
import org.iweb.sys.Parameters;
import org.iweb.sys.ToolsUtil;

import com.opensymphony.xwork2.ActionSupport;
import com.qkj.finishwinewl.dao.finishwinewlDao;
import com.qkj.finishwinewl.bean.finishwinewlBean;

/**
 * @author yanluhai 
 *
 */
public class finishwinewlAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(finishwinewlAction.class);
	private Map map = new HashMap();
	private finishwinewlDao dao = new finishwinewlDao();
	private finishwinewlBean finishwinewl;
	private List finishwinewls;
	private String message;
	private String viewFlag;
	private int recCount;
	private int pageSize;
	private int currPage;
	private String path = "首页 > 列表";
	private String []  imageUpload;
	private File fileUpload;
	private String fileUploadFileName; // 文件名+FileName
	
	
	
	
	

	public File getFileUpload() {
		return fileUpload;
	}

	public void setFileUpload(File fileUpload) {
		this.fileUpload = fileUpload;
	}

	public static Log getLog() {
		return log;
	}

	public static void setLog(Log log) {
		finishwinewlAction.log = log;
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public finishwinewlDao getDao() {
		return dao;
	}

	public void setDao(finishwinewlDao dao) {
		this.dao = dao;
	}

	public finishwinewlBean getFinishwinewl() {
		return finishwinewl;
	}

	public void setFinishwinewl(finishwinewlBean finishwinewl) {
		this.finishwinewl = finishwinewl;
	}

	public List getFinishwinewls() {
		return finishwinewls;
	}

	public void setFinishwinewls(List finishwinewls) {
		this.finishwinewls = finishwinewls;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getViewFlag() {
		return viewFlag;
	}

	public void setViewFlag(String viewFlag) {
		this.viewFlag = viewFlag;
	}

	public int getRecCount() {
		return recCount;
	}

	public void setRecCount(int recCount) {
		this.recCount = recCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String[] getImageUpload() {
		return imageUpload;
	}

	public void setImageUpload(String[] imageUpload) {
		this.imageUpload = imageUpload;
	}

	

	public String getFileUploadFileName() {
		return fileUploadFileName;
	}

	public void setFileUploadFileName(String fileUploadFileName) {
		this.fileUploadFileName = fileUploadFileName;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String list() throws Exception {
		ContextHelper.isPermit("QKJ_FINISHWINEWL_FINISHWINEWL_LIST");
		try {
			map.clear();
			if (finishwinewl == null) finishwinewl = new finishwinewlBean();
			ContextHelper.setSearchDeptPermit4Search("QKJ_FINISHWINEWL_FINISHWINEWL_LIST", map, "apply_depts", "apply_user");
			ContextHelper.SimpleSearchMap4Page("QKJ_FINISHWINEWL_FINISHWINEWL_LIST", map, finishwinewl, viewFlag);
			this.setPageSize(ContextHelper.getPageSize(map));
			this.setCurrPage(ContextHelper.getCurrPage(map));
			this.setFinishwinewls(dao.list(map));
			this.setRecCount(dao.getResultCount());
			path = "首页 > 列表";
		} catch (Exception e) {
			log.error(this.getClass().getName() + "!list 读取数据错误:", e);
			throw new Exception(this.getClass().getName() + "!list 读取数据错误:", e);
		}
		return SUCCESS;
	}

	public String load() throws Exception {
		try {
			if (null == viewFlag) {
				this.setFinishwinewl(null);
				setMessage("你没有选择任何操作!");
			} else if ("add".equals(viewFlag)) {
				
				this.setFinishwinewl(null);
				path = "首页 > 列表 > 增加";
			} else if ("mdy".equals(viewFlag)) {
				if (!(finishwinewl == null || finishwinewl.getUuid() == null)) {
					this.setFinishwinewl((finishwinewlBean) dao.get(finishwinewl.getUuid()));
					System.out.println(finishwinewl.getImage());
				} else {
					this.setFinishwinewl(null);
				}
				path = "首页 > 列表 > 修改产品";
			} else {
				this.setFinishwinewl(null);
				setMessage("无操作类型!");
			}
		} catch (Exception e) {
			log.error(this.getClass().getName() + "!load 读取数据错误:", e);
			throw new Exception(this.getClass().getName() + "!load 读取数据错误:", e);
		}
		return SUCCESS;
	}

	public String add() throws Exception {
		ContextHelper.isPermit("QKJ_FINISHWINEWL_FINISHWINEWL_ADD");
		try {
			 
			  
			/*finishwinewl.setAdd_user(ContextHelper.getUserLoginUuid());
			finishwinewl.setAdd_time(new Date());*/
			//String c = fileUpload.getParent();
			//File mm = new File(c + File.separator + fileUploadFileName);
			//copyFile(fileUpload.toString(), mm.toString());
			  
			fileUploadFileName="9FBA2651-8851-4a92-88C7-36BDA03856E9.png";
			String UploadRoot="";
			String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
		    //int num=t.indexOf(".metadata");
		    //String realpath=t.substring(1,num).replace('/', '\\')+"yxzx\\WebContent\\Imageupload";
		   // String realpath = ServletActionContext.getServletContext().getRealPath("./") + File.separator + "Imageupload";
		   // String realpath =  "u01/wwwhome/Imageupload";
		    //String realpath =  "u01" + File.separator +"wwwhome" + File.separator +"Imageupload";
		    //String realpath =  "E:\\hhh\\kk";
			String realpath = ServletActionContext.getServletContext().getRealPath("./") + File.separator + "Imageupload";
			 if (fileUploadFileName != null)
			 {
				         
					//imageUpload = fileUploadFileName.split(",");
					/*for(int i=0;i

5.前端页面


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="it" uri="http://qkjchina.com/iweb/iwebTags" %>




列表--<s:text name="APP_NAME" />





物料编码 物料名称 香型 检测信息 质量标准 零售价 品鉴信息 建议饮用方式 产品介绍 检测报告编号 操作
${code} ${name} ${fragrance} ${introduction} ${detection_info} ${quality_standard } ${retail_price} ${tasting_info} ${drinking_method} ${report_number} mdy">修改 " onclick="return isDel();">删除 查看

你可能感兴趣的:(Java)