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

您所在的位置:网站首页 asp下载数据库中文件 关于前端文件上传后将文件保存至服务器路径存储在数据库并在相应页面展示的总结

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

2024-06-21 04:31| 来源: 网络整理| 查看: 265

前期准备

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

具体实例展示

1.数据库建表

SQLsever可视化工具 其中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 修改 删除 查看 $(function(){ printPagination("listpage",'${currPage}','${recCount}','${pageSize}'); });

此页用列表的方式展示相应信息

产品管理-- ${path} 返回列表 fid: 物料编码: 香型: 检测信息: 质量标准: 零售价: 品鉴信息: 建议饮用方式: 产品介绍: 检测报告编号: 产品图片: ${image} 产品图片: 相关操作:

在这里插入图片描述 最终可以看到我们上传的各项数据及文件(以图片为例)展示在页面上。 明细页实现前端收集数据和展示文件的功能

接下来介绍实现文件上传的几个关键点

1.前端文件如何选择:

产品图片:

其中fileUpload暂存上传的文件, 在这里插入图片描述 在后端代码中定义file 类型的fileUpload,写好它的get(),set()方法,想象一下我们已经在前端将一个文件传到现在的后端fileUpload中,现在要做的是将文件上传到服务器指定文件夹或者项目的某个文件夹中,要将这个路径存放到我们的数据库相应字段下面。 下面实现的就是将文件上传到项目文件夹以及将路径存储到数据库的过程。

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(); String realpath = ServletActionContext.getServletContext().getRealPath("./") + File.separator + "Imageupload"; if (fileUploadFileName != null) { //imageUpload = fileUploadFileName.split(","); /*for(int i=0;i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3