`

bboss mvc文件上传下载实战演练

阅读更多
本文以一个实际的demo工程来介绍,如何通过bbossgroups来实现以下功能:
1.通过MVC实现文件上传,通过持久层框架将文件存入数据库
2.使用持久层框架从数据库中获取文件实现下载功能(blob下载和转储为File下载两种方式)
3.本文涉及框架模块:mvc,persistent,taglib,aop/ioc



   本文展示如何从MultipartHttpServletRequest中获取上传的附件,  后续将撰文介绍直接绑定MultipartFile对象或者数组到控制其方法参数或者po对象属性的案例。
   本文采用derby数据库。建库的脚本为:
  
CREATE TABLE FILETABLE ( FILENAME VARCHAR(100), FILECONTENT BLOB(2147483647),
 FILEID VARCHAR(100), FILESIZE BIGINT )

一、实战
1.下载的最新的bboss eclipse工程
bboss
2.解压后将eclipse工程bestpractice\bbossupload导入eclipse

3.修改/bbossupload/src/poolman.xml中derby数据库文件路径:
<url>jdbc:derby:D:/d/workspace/bbossgroups-3.6.0/bestpractice/bbossupload/database/cimdb</url>
其中的D:/d/workspace/bbossgroups-3.6.0/bestpractice/bbossupload/database/cimdb需要修改为你的工程所在的实际路径

4.准备好tomcat 6和jdk 6或以上
5.在tomcat 6的conf\Catalina\localhost下增加upload.xml文件,内容为(路径需要调整为实际路径):
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="D:\workspace\bbossgroups-3.5\bestpractice\bbossupload\WebRoot" path="/upload" debug="0" reloadable="false">
</Context>
用户可以根据自己的情况设置docBase属性的值

6.启动tomcat,输入以下地址即可访问bboss mvc的附件上传下载实例了:
http://localhost:8080/upload/upload/main.page
效果如下:






二、代码赏析
1.在bboss-mvc.xml中增加以下文件上传插件配置:
<property name="multipartResolver"   f:encoding="UTF-8"
         class="org.frameworkset.web.multipart.commons.CommonsMultipartResolver">   

在bboss-mvc.xml中增加下载插件配置-FileMessageConvertor
 <property name="httpMessageConverters">
     	<list>
     		<property class="org.frameworkset.http.converter.json.MappingJacksonHttpMessageConverter"/>
     		<property class="org.frameworkset.http.converter.StringHttpMessageConverter"/>
     		<property class="org.frameworkset.http.converter.FileMessageConvertor"/>
     	</list>        
     </property> 


2.文件上传下载控制器装配文件
<?xml version="1.0" encoding='UTF-8'?>
<properties>
	<property name="/upload/*.page"				
		path:main="/upload.jsp"
		path:ok="redirect:main.page"
		class="org.frameworkset.upload.controller.UploadController"
		f:uploadService="attr:uploadService"
		/>		
	<property name="uploadService"
		class="org.frameworkset.upload.service.UploadService" 
		f:uploadDao="attr:uploadDao"/>
	<property name="uploadDao"
		class="org.frameworkset.upload.dao.impl.UpLoadDaoImpl" />
	<!-- 
	如果启用配置文件方式,则打开以下注释,注释上述uploadDao即可
	 -->
	<!-- 
	<property name="uploadDao"
		class="org.frameworkset.upload.dao.impl.ConfigSQLUploadDaoImpl" 
		f:executor="attr:uploadDao.configexecutor"/>	
	<property name="uploadDao.configexecutor" class="com.frameworkset.common.poolman.ConfigSQLExecutor">
		<construction>
			<property value="org/frameworkset/upload/dao/impl/uploadsql.xml"/>
		</construction>
     </property> 
      -->
</properties>


3.控制器实现类代码-UploadController
package org.frameworkset.upload.controller;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.frameworkset.upload.service.FileBean;
import org.frameworkset.upload.service.UploadService;
import org.frameworkset.util.annotations.RequestParam;
import org.frameworkset.util.annotations.ResponseBody;
import org.frameworkset.web.multipart.MultipartFile;
import org.frameworkset.web.multipart.MultipartHttpServletRequest;
import org.frameworkset.web.servlet.ModelMap;

/**
 * CREATE TABLE FILETABLE ( FILENAME VARCHAR(100), FILECONTENT BLOB(2147483647),
 * FILEID VARCHAR(100), FILESIZE BIGINT )
 * 
 * <p>
 * XMLRequestController.java
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * bboss workgroup
 * </p>
 * <p>
 * Copyright (c) 2009
 * </p>
 * 
 * @Date 2011-6-22
 * @author biaoping.yin
 * @version 1.0
 */
public class UploadController
{

	private UploadService	uploadService;

	public String main(ModelMap model) throws Exception
	{

		try
		{
			model.addAttribute("files", uploadService.queryfiles());
			model.addAttribute("clobfiles", uploadService.queryclobfiles());
		}
		catch (Exception e)
		{
			throw e;
		}
		return "path:main";
	}

	/**
	 * @param request
	 * @param model
	 * @param idNum
	 * @param type
	 * @param des
	 * @param byid
	 * @return
	 */
	public String uploadFile(MultipartHttpServletRequest request)
	{
		Iterator<String> fileNames = request.getFileNames();
		// 根据服务器的文件保存地址和原文件名创建目录文件全路径		
		try
		{
			while (fileNames.hasNext())
			{
				String name = fileNames.next();
				MultipartFile[] files = request.getFiles(name);				 
//				file.transferTo(dest)
				for(MultipartFile file:files)
				{
					String filename = file.getOriginalFilename();
					if (filename != null && filename.trim().length() > 0)
					{
						uploadService.uploadFile(file.getInputStream(), file
								.getSize(), filename);
					}
				}
			}			
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		return "path:ok";
	}
	
	
	/**
	 * 
	 * @param upload1 参数名称要和<input type="file" id="upload1" name="upload1" style="width: 200px"/>
	 *        中的name属性保持一致,这样就能够自动进行绑定和映射
	 * @return
	 */
//	public String uploadFileWithMultipartFile(@RequestParam(name="upload1")  MultipartFile file)
	public String uploadFileWithMultipartFile(MultipartFile upload1)
	{

		
		// 根据服务器的文件保存地址和原文件名创建目录文件全路径
		
		try
		{
			String filename = upload1.getOriginalFilename();
			if (filename != null && filename.trim().length() > 0)
			{
				uploadService.uploadFile(upload1.getInputStream(), upload1
						.getSize(), filename);

			}			
			
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		return "path:ok";
	}
	
	public String uploadFileClobWithMultipartFile(MultipartFile upload1)
	{
		try
		{
			uploadService.uploadClobFile(upload1);			
			
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		return "path:ok";
	}
	
//	public @ResponseBody File uploaddownFileWithMultipartFile( MultipartFile file) throws IllegalStateException, IOException
	/**
	 * 
	 * @param upload1 参数名称要和<input type="file" id="upload1" name="upload1" style="width: 200px"/>
	 *        中的name属性保持一致
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	public @ResponseBody File uploaddownFileWithMultipartFile( MultipartFile upload1) throws IllegalStateException, IOException
	{

		File f = new File("d:/" + upload1.getOriginalFilename());
		upload1.transferTo(f);
		return f;
		// 根据服务器的文件保存地址和原文件名创建目录文件全路径
		
//		try
//		{
//			String filename = file.getOriginalFilename();
//			if (filename != null && filename.trim().length() > 0)
//			{
//				uploadService.uploadFile(file.getInputStream(), file
//						.getSize(), filename);
//
//			}			
//			
//		}
//		catch (Exception ex)
//		{
//			ex.printStackTrace();
//		}
//		return "path:ok";
	}
	
	/**
	 * @param request
	 * @param model
	 * @param idNum
	 * @param type
	 * @param des
	 * @param byid
	 * @return
	 */
	public String uploadFileWithListBean(List<FileBean> files)
	{			
		
		return "path:ok";
	}
	
	/**
	 * 
	 * @param upload1 参数名称要和<input type="file" id="upload1" name="upload1" style="width: 200px"/>
	 *        中的name属性保持一致,否则就需要@RequestParam来建立映射关系
	 * @return
	 */
//	public String uploadFileWithMultipartFiles(@RequestParam(name="upload1")  MultipartFile[] files)
	public String uploadFileWithMultipartFiles(MultipartFile[] upload1)
	{		
		try
		{ 
			
			for(MultipartFile file:upload1)
			{
				String filename = file.getOriginalFilename();
//				file.transferTo(new File("d:/"+ filename));
				
				if (filename != null && filename.trim().length() > 0)
				{
					uploadService.uploadFile(file.getInputStream(), file
							.getSize(), filename);
	
				}
			}			
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		return "path:ok";
	}
	
	/**
	 * 
	 * @param upload1 参数名称要和<input type="file" id="upload1" name="upload1" style="width: 200px"/>
	 *        中的name属性保持一致,否则就需要@RequestParam来建立映射关系
	 * @return
	 */
//	public @ResponseBody(charset="UTF-8") String uploadFileWithMultipartFilesJson(@RequestParam(name="upload1")  MultipartFile[] upload1)
	public @ResponseBody(charset="UTF-8") String uploadFileWithMultipartFilesJson(MultipartFile[] upload1)
	{		
		try
		{ 

			for(MultipartFile file:upload1)
			{
				String filename = file.getOriginalFilename();
//				file.transferTo(new File("d:/"+ filename));
				
				if (filename != null && filename.trim().length() > 0)
				{
					uploadService.uploadFile(file.getInputStream(), file
							.getSize(), filename);
	
				}
			}			
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		return "你好";
	}
	
	/**
	 * @param request
	 * @param model
	 * @param idNum
	 * @param type
	 * @param des
	 * @param byid
	 * @return
	 */
	public String uploadFileWithFileBean(FileBean file)
	{	
		try
		{
			
			//对FileBean对象中的附件进行处理。。。。
			
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		return "path:ok";
	}

	public String deletefiles() throws Exception
	{

		uploadService.deletefiles();
		return "path:ok";
	}

	public String queryfiles() throws Exception
	{

		
		return "path:ok";
	}

	/**
	 * 直接将blob对应的文件内容以相应的文件名响应到客户端,需要提供request和response对象
	 * 这个方法比较特殊,因为derby数据库的blob字段必须在statement有效范围内才能使用,所以采用了空行处理器,来进行处理
	 * 查询数据库的操作也只好放在控制器中处理
	 * @param fileid 参数名称要和request请求中的参数名称保持一致,否则就需要@RequestParam来建立映射关系
	 * @param request
	 * @param response
	 * @throws Exception
	 */
//	public void downloadFileFromBlob(
//			@RequestParam(name = "fileid") String fileid,
//			 HttpServletRequest request, HttpServletResponse response)
//			throws Exception
	public void downloadFileFromBlob(
			String fileid,
			 HttpServletRequest request, HttpServletResponse response)
			throws Exception
	{
		uploadService.downloadFileFromBlob(fileid, request, response);

	}
	
	/**
	 * 直接将blob对应的文件内容以相应的文件名响应到客户端,需要提供request和response对象
	 * 这个方法比较特殊,因为derby数据库的blob字段必须在statement有效范围内才能使用,所以采用了空行处理器,来进行处理
	 * 查询数据库的操作也只好放在控制器中处理
	 * @param fileid 用来验证指定@RequestParam注解的参数,但是RequestParam并没明显地指定参数的名称,则将会用方法参数的名称
	 * 作为参数名称
	 * @param request
	 * @param response
	 * @throws Exception
	 */
//	public void downloadFileFromClob(
//			@RequestParam(name = "fileid") String fileid,
//			 HttpServletRequest request, HttpServletResponse response)
//			throws Exception
	public void downloadFileFromClob(
			@RequestParam String fileid,
			 HttpServletRequest request, HttpServletResponse response)
			throws Exception
	{

		uploadService.downloadFileFromClob(fileid, request, response);

	}

	/**
	 * 将数据库中存储的文件内容转储到应用服务器文件目录中,然后将转储的文件下载,无需提供response和request对象
	 * 
	 * @param fileid
	 * @return
	 * @throws Exception
	 */
	public @ResponseBody File downloadFileFromFile(String fileid)
			throws Exception
	{

		return uploadService.getDownloadFile(fileid);
	}
	public @ResponseBody File downloadFileFromClobFile(String fileid)
	throws Exception
	{
	
		return uploadService.getDownloadClobFile(fileid);
	}
	

	/**
	 * bbossgroups 3.5版本对aop框架的属性注入功能做了改进,注入的属性无需再定义get/set方法
	 */
//	public UploadService getUploadService()
//	{
//
//		return uploadService;
//	}
//
//	public void setUploadService(UploadService uploadService)
//	{
//
//		this.uploadService = uploadService;
//	}

}


4.业务组件-UploadService
 package org.frameworkset.upload.service;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.frameworkset.upload.dao.UpLoadDao;
import org.frameworkset.web.multipart.MultipartFile;


/**
 * <p>UploadService.java</p>
 * <p> Description: </p>
 * <p> bboss workgroup </p>
 * <p> Copyright (c) 2009 </p>
 * 
 * @Date 2011-6-17
 * @author biaoping.yin
 * @version 1.0
 */
public class UploadService
{
	private UpLoadDao uploadDao;
	public UpLoadDao getUploadDao() {
		return uploadDao;
	}
	public void setUploadDao(UpLoadDao uploadDao) {
		this.uploadDao = uploadDao;
	}
	public void deletefiles() throws Exception
	{

		uploadDao.deletefiles();
		
	}
	public List<HashMap> queryfiles() throws Exception
	{

		// TODO Auto-generated method stub
		return uploadDao.queryfiles();
	}
	public void uploadFile(InputStream inputStream, long size, String filename) throws Exception
	{

		uploadDao.uploadFile(inputStream, size, filename);
		
	}
	
	
	
	public void uploadClobFile(MultipartFile file) throws Exception
	{

		uploadDao.uploadClobFile(file);
		
	}
	
	public File getDownloadFile(String fileid) throws Exception
	{
		return uploadDao.getDownloadFile(fileid);
	}
	
	public void downloadFileFromBlob(String fileid,  HttpServletRequest request,
			HttpServletResponse response) throws Exception
	{
		uploadDao.downloadFileFromBlob(fileid, request, response);
	}
	
	public void downloadFileFromClob(String fileid,  HttpServletRequest request,
			HttpServletResponse response) throws Exception
	{
		uploadDao.downloadFileFromClob(fileid, request, response);
	}
	public  List<HashMap> queryclobfiles() throws Exception
	{

		// TODO Auto-generated method stub
		return uploadDao.queryclobfiles();
	}
	public File getDownloadClobFile(String fileid) throws Exception
	{

		return uploadDao.getDownloadClobFile(fileid);
	}
}



5.dao组件代码-
package org.frameworkset.upload.dao.impl;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.frameworkset.upload.dao.UpLoadDao;

import com.frameworkset.common.poolman.Record;
import com.frameworkset.common.poolman.SQLExecutor;
import com.frameworkset.common.poolman.SQLParams;
import com.frameworkset.common.poolman.handle.FieldRowHandler;
import com.frameworkset.common.poolman.handle.NullRowHandler;
import com.frameworkset.util.StringUtil;

public class UpLoadDaoImpl implements UpLoadDao {
	/**
	 * 上传附件
	 * @param inputStream
	 * @param filename
	 * @return
	 * @throws Exception
	 */
	public boolean uploadFile(InputStream inputStream,long size, String filename) throws Exception {
		boolean result = true;
		String sql = "";
		try {
			sql = "INSERT INTO filetable (FILENAME,FILECONTENT,fileid,FILESIZE) VALUES(#[filename],#[FILECONTENT],#[FILEID],#[FILESIZE])";
			SQLParams sqlparams = new SQLParams();
			sqlparams.addSQLParam("filename", filename, SQLParams.STRING);
			sqlparams.addSQLParam("FILECONTENT", inputStream, size,SQLParams.BLOBFILE);
			sqlparams.addSQLParam("FILEID", UUID.randomUUID().toString(),SQLParams.STRING);
			sqlparams.addSQLParam("FILESIZE", size,SQLParams.LONG);
			SQLExecutor.insertBean(sql, sqlparams);			
			
		} catch (Exception ex) {
			ex.printStackTrace();
			result = false;
			throw new Exception("上传附件关联临控指令布控信息附件失败:" + ex);
		} finally {
			if(inputStream != null){
				inputStream.close();
			}
		}
		return result;
	}
	
	public File getDownloadFile(String fileid) throws Exception
	{
		try
		{
			return SQLExecutor.queryTField(
											File.class,
											new FieldRowHandler<File>() {

												@Override
												public File handleField(
														Record record)
														throws Exception
												{

													// 定义文件对象
													File f = new File("d:/",record.getString("filename"));
													// 如果文件已经存在则直接返回f
													if (f.exists())
														return f;
													// 将blob中的文件内容存储到文件中
													record.getFile("filecontent",f);
													return f;
												}
											},
											"select * from filetable where fileid=?",
											fileid);
		}
		catch (Exception e)
		{
			throw e;
		}
	}

	@Override
	public void deletefiles() throws Exception
	{

		SQLExecutor.delete("delete from filetable ");			
	}

	@Override
	public List<HashMap> queryfiles() throws Exception
	{

		return SQLExecutor.queryList(HashMap.class, "select FILENAME,fileid,FILESIZE from filetable");
		
	}

	@Override
	public void downloadFileFromBlob(String fileid, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception
	{

		try
		{
			SQLExecutor.queryByNullRowHandler(new NullRowHandler() {
				@Override
				public void handleRow(Record record) throws Exception
				{

					StringUtil.sendFile(request, response, record
							.getString("filename"), record
							.getBlob("filecontent"));
				}
			}, "select * from filetable where fileid=?", fileid);
		}
		catch (Exception e)
		{
			throw e;
		}
		
	}
	
	
	
}


6.表单界面代码-
<h3>MultipartHttpServletRequest方式</h3>
		<form name="tableInfoForm" id="tableInfoForm" enctype="multipart/form-data" method="post" action="<%=request.getContextPath() %>/upload/uploadFile.page"> 
			<input type="hidden" value="0" name="type"/>
			<table width="600" border="0" cellspacing="0" cellpadding="0" class="tox5 bgcolor1">
				<tr>
					<td>
						<table id="upfileList" align="center">
							<tr>
								<td height="30">文件:<font style="color:#FF0000">*</font></td>
								<td><input type="file" id="upload1" name="upload1" style="width: 200px"/></td>
								<td><input type="file" id="upload1" name="upload1" style="width: 200px"/></td>								
							</tr>													
						</table>
						<div align=center>
							
							<a href="<%=request.getContextPath() %>/upload/deletefiles.page" >删除</a>
							<input type="submit" class="xbutton1" id="subb" name="subb" value="上 传"/>
						</div>
					</td>
				</tr>
			</table>
		</form>	
		
		<h3>MultipartFile方式</h3>
		<form name="tableInfoForm1" id="tableInfoForm1" enctype="multipart/form-data" method="post" action="<%=request.getContextPath() %>/upload/uploadFileWithMultipartFile.page"> 
			<input type="hidden" value="0" name="type"/>
			<table width="600" border="0" cellspacing="0" cellpadding="0" class="tox5 bgcolor1">
				<tr>
					<td>
						<table id="upfileList" align="center">
							<tr>
								<td height="30">文件:<font style="color:#FF0000">*</font></td>
								<td><input type="file" id="upload1" name="upload1" style="width: 200px"/></td>
								<td><input type="file" id="upload1" name="upload1" style="width: 200px"/></td>								
							</tr>													
						</table>
						<div align=center>
							
							
							<input type="submit" class="xbutton1" id="subb" name="subb" value="上 传"/>
						</div>
					</td>
				</tr>
			</table>
		</form>	
		
		<h3>MultipartFile[]方式</h3>
		<form name="tableInfoForm2" id="tableInfoForm2" enctype="multipart/form-data" method="post" 
		action="<%=request.getContextPath() %>/upload/uploadFileWithMultipartFiles.page"> 
			<input type="hidden" value="0" name="type"/>
			<table width="600" border="0" cellspacing="0" cellpadding="0" class="tox5 bgcolor1">
				<tr>
					<td>
						<table id="upfileList" align="center">
							<tr>
								<td height="30">文件:<font style="color:#FF0000">*</font></td>
								<td><input type="file" id="upload1" name="upload1" style="width: 200px"/></td>
								<td><input type="file" id="upload1" name="upload1" style="width: 200px"/></td>								
							</tr>													
						</table>
						<div align=center>
							
							
							<input type="submit" class="xbutton1" id="subb" name="subb" value="上 传"/>
						</div>
					</td>
				</tr>
			</table>
		</form>	
		
		<h3>List BeanMultipartFile方式</h3>
		<form name="tableInfoForm21" id="tableInfoForm21" enctype="multipart/form-data" method="post" 
		action="<%=request.getContextPath() %>/upload/uploadFileWithListBean.page"> 
			<input type="hidden" value="0" name="type"/>
			<table width="600" border="0" cellspacing="0" cellpadding="0" class="tox5 bgcolor1">
				<tr>
					<td>
						<table id="upfileList" align="center">
							<tr>
								<td height="30">文件:<font style="color:#FF0000">*</font></td>
								<td><input type="file" id="upload1" name="upload1" style="width: 200px"/> <input type="text" name="upload1des" style="width: 200px"/></td>
								<td><input type="file" id="upload1" name="upload1" style="width: 200px"/> <input type="text" name="upload1des" style="width: 200px"/></td>	
															
							</tr>													
						</table>
						<div align=center>
							
							
							<input type="submit" class="xbutton1" id="subb" name="subb" value="上 传"/>
						</div>
					</td>
				</tr>
			</table>
		</form>	
		
		
		<h3>FileBean方式</h3>
		<form name="tableInfoForm3" id="tableInfoForm3" enctype="multipart/form-data" method="post" action="<%=request.getContextPath() %>/upload/uploadFileWithFileBean.page"> 
			<input type="hidden" value="0" name="type"/>
			<table width="600" border="0" cellspacing="0" cellpadding="0" class="tox5 bgcolor1">
				<tr>
					<td>
						<table id="upfileList" align="center">
							<tr>
								<td height="30">文件:<font style="color:#FF0000">*</font></td>
								<td><input type="file" id="upload1" name="upload1" style="width: 200px"/></td>
								<td><input type="file" id="upload1" name="upload1" style="width: 200px"/></td>								
							</tr>													
						</table>
						<div align=center>
							
							
							<input type="submit" class="xbutton1" id="subb" name="subb" value="上 传"/>
						</div>
					</td>
				</tr>
			</table>
		</form>	
		
			<h3>clob multipartfile</h3>
		<form name="tableInfoForm4" id="tableInfoForm4" enctype="multipart/form-data" method="post" action="<%=request.getContextPath() %>/upload/uploadFileClobWithMultipartFile.page"> 
			<input type="hidden" value="0" name="type"/>
			<table width="600" border="0" cellspacing="0" cellpadding="0" class="tox5 bgcolor1">
				<tr>
					<td>
						<table id="upfileList" align="center">
							<tr>
								<td height="30">文件:<font style="color:#FF0000">*</font></td>
								<td><input type="file" id="upload1" name="upload1" style="width: 200px"/></td>
																
							</tr>													
						</table>
						<div align=center>
							
							
							<input type="submit" class="xbutton1" id="subb" name="subb" value="上 传"/>
						</div>
					</td>
				</tr>
			</table>
		</form>	
		
			<h3>upload download multipartfile</h3>
		<form name="tableInfoForm4" id="tableInfoForm5" enctype="multipart/form-data" 
		method="post" action="<%=request.getContextPath() %>/upload/uploaddownFileWithMultipartFile.page"> 
			<input type="hidden" value="0" name="type"/>
			<table width="600" border="0" cellspacing="0" cellpadding="0" class="tox5 bgcolor1">
				<tr>
					<td>
						<table id="upfileList" align="center">
							<tr>
								<td height="30">文件:<font style="color:#FF0000">*</font></td>
								<td><input type="file" id="upload1" name="upload1" style="width: 200px"/></td>
																
							</tr>													
						</table>
						<div align=center>
							
							
							<input type="submit" class="xbutton1" id="subb" name="subb" value="上 传"/>
						</div>
						
					</td>
				</tr>
			</table>
		</form>	
		<table>
		<tr><td>blob文件名称</td><td>文件下载</td></tr>
		<pg:empty actual="${files}"><tr><td colspan="2">没有文件信息</td></tr></pg:empty>
		<pg:list requestKey="files">
			<tr><td><pg:cell colName="FILENAME"/></td>
			<td><a href="<%=request.getContextPath() %>/upload/downloadFileFromBlob.page?fileid=<pg:cell colName="FILEID"/>">
			blob方式下载</a>
			<a href="<%=request.getContextPath() %>/upload/downloadFileFromFile.page?fileid=<pg:cell colName="FILEID"/>">
			blob转储为文件方式下载</a>
   		</td></tr>
		</pg:list>
		</table>
		
		<table>
		<tr><td>clob文件名称</td><td>文件下载</td></tr>
		<pg:empty actual="${clobfiles}"><tr><td colspan="2">没有文件信息</td></tr></pg:empty>
		<pg:list requestKey="clobfiles">
			<tr><td><pg:cell colName="FILENAME"/></td>
			<td><a href="<%=request.getContextPath() %>/upload/downloadFileFromClob.page?fileid=<pg:cell colName="FILEID"/>">
			clob方式下载</a>
			<a href="<%=request.getContextPath() %>/upload/downloadFileFromClobFile.page?fileid=<pg:cell colName="FILEID"/>">
			clob转储为文件方式下载</a>
		</td></tr>
		</pg:list>
		</table>


7.将dao切换为采用配置文件dao-ConfigSQLUploadDaoImpl
/**
 *  Copyright 2008 biaoping.yin
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.  
 */
package org.frameworkset.upload.dao.impl;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.frameworkset.upload.dao.UpLoadDao;

import com.frameworkset.common.poolman.ConfigSQLExecutor;
import com.frameworkset.common.poolman.Record;
import com.frameworkset.common.poolman.SQLParams;
import com.frameworkset.common.poolman.handle.FieldRowHandler;
import com.frameworkset.common.poolman.handle.NullRowHandler;
import com.frameworkset.util.StringUtil;



/**
 * <p>ConfigSQLUploadDaoImpl.java</p>
 * <p> Description: </p>
 * <p> bboss workgroup </p>
 * <p> Copyright (c) 2009 </p>
 * 
 * @Date 2011-6-17
 * @author biaoping.yin
 * @version 1.0
 */
public class ConfigSQLUploadDaoImpl implements UpLoadDao
{
	private ConfigSQLExecutor executor ;
	/**
	 * 上传附件
	 * @param inputStream
	 * @param filename
	 * @return
	 * @throws Exception
	 */
	public boolean uploadFile(InputStream inputStream,long size, String filename) throws Exception {
		boolean result = true;
		
		try {
			
			SQLParams sqlparams = new SQLParams();
			sqlparams.addSQLParam("filename", filename, SQLParams.STRING);
			sqlparams.addSQLParam("FILECONTENT", inputStream, size,SQLParams.BLOBFILE);
			sqlparams.addSQLParam("FILEID", UUID.randomUUID().toString(),SQLParams.STRING);
			sqlparams.addSQLParam("FILESIZE", size,SQLParams.LONG);
			executor.insertBean("uploadFile", sqlparams);			
			
		} catch (Exception ex) {
			ex.printStackTrace();
			result = false;
			throw new Exception("上传附件关联临控指令布控信息附件失败:" + ex);
		} finally {
			if(inputStream != null){
				inputStream.close();
			}
		}
		return result;
	}
	
	public File getDownloadFile(String fileid) throws Exception
	{
		try
		{
			return executor.queryTField(
											File.class,
											new FieldRowHandler<File>() {

												@Override
												public File handleField(
														Record record)
														throws Exception
												{

													// 定义文件对象
													File f = new File(
															"d:/",
															record
																	.getString("filename"));
													// 如果文件已经存在则直接返回f
													if (f.exists())
														return f;
													// 将blob中的文件内容存储到文件中
													record
															.getFile(
																		"filecontent",
																		f);
													return f;
												}
											},
											"getDownloadFile",
											fileid);
		}
		catch (Exception e)
		{
			throw e;
		}
	}

	@Override
	public void deletefiles() throws Exception
	{

		executor.delete("deletefiles");			
	}

	@Override
	public List<HashMap> queryfiles() throws Exception
	{

		return executor.queryList(HashMap.class, "queryfiles");
		
	}

	@Override
	public void downloadFileFromBlob(String fileid, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception
	{

		try
		{
			executor.queryByNullRowHandler(new NullRowHandler() {
				@Override
				public void handleRow(Record record) throws Exception
				{

					StringUtil.sendFile(request, response, record
							.getString("filename"), record
							.getBlob("filecontent"));
				}
			}, "downloadFileFromBlob", fileid);
		}
		catch (Exception e)
		{
			throw e;
		}
		
	}

	
	public ConfigSQLExecutor getExecutor()
	{
	
		return executor;
	}

	
	public void setExecutor(ConfigSQLExecutor executor)
	{
	
		this.executor = executor;
	}

}


sql配置文件内容-
<?xml version="1.0" encoding='gb2312'?>
<properties>
	<property name="deletefiles"><![CDATA[delete from filetable]]>
	</property>
	<property name="queryfiles"><![CDATA[select FILENAME,fileid,FILESIZE from filetable]]>
	</property>
	<property name="uploadFile"><![CDATA[
		INSERT INTO filetable (FILENAME,FILECONTENT,fileid,FILESIZE) 
		VALUES(#[filename],#[FILECONTENT],#[FILEID],#[FILESIZE])]]>
	</property>
	
	<property name="getDownloadFile"><![CDATA[
		select * from filetable where fileid=?
	]]></property>
	
	
</properties>



代码都很简单,也非常容易理解,这里不做过多的解释。有问题可以留言讨论,也可以加入群组:
21220580
3625720
154752521
官方网站:
http://www.bbossgroups.com/
  • 大小: 15.9 KB
  • 大小: 12.8 KB
  • 大小: 13.1 KB
分享到:
评论
2 楼 yin_bp 2014-11-10  
huangjia7 写道
没有看懂 upload.xml配置有什么用,我现在想把一张图片保存到服务器的某个文件夹,请问用bboss提供的方法怎么做?

upload.xml是一个应用部署描述文件,你不需要关注,如果要上传一个图片到服务某个文件夹,可以如下编写控制器方法:

 public @ResponseBody String uploaddownFileWithMultipartFile( MultipartFile picture,String name,String sex) throws IllegalStateException, IOException   
{   
  
//设定要保存的服务器文件路径
        File f = new File("d:/" + upload1.getOriginalFilename   
        picture.transferTo(f);   //将上传的文件保存到对应的服务器文件中
        return "sucess";   
}


客户端js提交函数如下:

var url = "${pageContext.request.contextPath}/file/uploaddownFileWithMultipartFile.page";
     $("#upfile").form('submit', {
				    "url": url,
				    onSubmit:function(){			
						
				    },
				    success:function(responseText){	
				    							
						if(responseText == "success"){
							alert("提示对话框 , 增加文件成功!");
							
						}
						else{
							alert("提示对话框 , 增加文件失败:"+responseText);
						}
								
				    }
				});	


$("#upfile").form部分upfile是指页面上的form id值
1 楼 huangjia7 2014-11-10  
没有看懂 upload.xml配置有什么用,我现在想把一张图片保存到服务器的某个文件夹,请问用bboss提供的方法怎么做?

相关推荐

    bboss mvc文件上传下载实战进阶

    NULL 博文链接:https://yin-bp.iteye.com/blog/1131637

    bboss mvcdemo 下载地址

    - 安装与配置指南:如何下载和设置BBoss MVCDemo环境,包括JDK、应用服务器(如Tomcat)、数据库等的配置。 - 快速入门:简单的示例展示如何创建第一个BBoss MVCDemo应用,包括编写控制器、模型和视图。 - 请求处理...

    bboss mvc 通过jsonp实现跨站跨域远程访问

    标题中的“bboss mvc 通过jsonp实现跨站跨域远程访问”指的是使用bboss MVC框架来处理JSONP(JSON with Padding)请求,以克服浏览器的同源策略限制,实现跨域数据交互。bboss MVC是一个Java Web开发框架,它提供了...

    bboss ioc配置文件中使用外部属性文件介绍

    本文将详细介绍如何在BBoss的IOC配置文件中引用外部属性文件,以便更好地管理和动态配置应用。 首先,BBoss的IOC配置文件通常是一个XML文件,比如`bboss-ioc.xml`,在这个文件中我们可以声明并配置各种bean。当需要...

    bboss-mvc.jar

    官方版本,亲测可用

    基于bboss框架的全面设计源码自动生成工具

    该工具是一款基于bboss...此工具能够自动生成bboss MVC、IOC、持久化、JSP、i18n、SQL配置文件、Web服务、Hessian服务等相关源代码,适用于快速开发和项目迭代。详细文档请参考:http://yin-bp.iteye.com/blog/2256948

    bboss-gencode:这是一个自动为bboss mvc,ioc,persistent,jsp,i18n,sql配置文件,webservice,hessian服务等生成源代码的工具。Dev文档yin-bp.iteye.comblog2256948-mvc source code

    自动代码生成器是针对bboss框架和bboss开发平台的自动代码生成工具,可以根据模板,自动生成给定表的增,删,改,分页查询,列表查询,国际化功能对应的程序和配置文件: 1.mvc控制器 2.业务组件 3.实体类 4.jsp文件...

    bbossgroups 开发系列文章之一 最佳实践

    bboss-mvc.xml是bboss MVC的核心配置文件,它定义了系统的全局设置,如视图解析器(View Resolver)和消息源(Message Source)。视图解析器负责将处理结果转换为用户可以查看的页面,例如JSP。在这里,配置了...

    springboot整合bboss es增删改查测试demo代码

    本实例是一个基于bboss es spring boot starter的demo maven工程,可供spring boot项目集成bboss elasticsearch rest client参考 展示了通过spring boot管理单集群功能和管理多集群功能 单集群测试用例:...

    基于Java的bboss开源框架设计源码

    该项目是基于Java语言的bboss开源框架构建的设计源码,包含3075个文件,涵盖2700个Java源文件、85个XML配置文件、52个HTML文件、39个properties和prefs文件、23个GIF图片、18个Gradle文件、17个gitignore文件、10个...

    bboss会话共享培训文档

    bboss会话共享是一种分布式会话管理技术,它针对在集群环境下应用部署时如何解决会话数据丢失和单点登录问题提供了专门的解决方案。根据提供的文档内容,我们可以详细探讨bboss会话共享涉及的关键知识点。 首先,...

    企业级J2EE开源框架bboss

    BBoss(全称为bbossgroups)是一个专为企业级J2EE应用设计的开源框架,它为Java开发者提供了一系列强大的工具和服务,以简化Web应用程序的开发过程。该框架旨在提高开发效率,降低维护成本,同时保持高度的灵活性和...

    bboss-elasticsearch开发环境搭建和开发入门视频教程.

    2. **下载Elasticsearch**: 从官网下载对应版本的 Elasticsearch 并解压至合适的位置。 3. **启动Elasticsearch**: 执行解压后的 bin 目录下的 `elasticsearch`(Windows)或 `./elasticsearch`(Linux/Mac)脚本,...

    bboss elasticsearch-5.7.8.rar

    3. `bboss-util-5.5.0.jar`和`bboss-persistent-5.5.0.jar`:这是BBoss框架的核心组件,提供了许多实用工具类和持久化操作支持,如数据库连接、事务管理等,为Elasticsearch的数据导入提供底层支持。 4. `...

    springboot集成ElasticsearchBboss调用Elasticsearch.md

    springboot集成ElasticsearchBboss调用Elasticsearch的案例分享

Global site tag (gtag.js) - Google Analytics