ITeye博客

struts2文档上传与下载

hanjava · 2012-07-03 19:21 · java 应用 · Web前端 · Markdown

页面(freemark页面):    excle模板下载

//文件上传操作 function addOperatorUpload(){ var filepath=document.getElementById("filePath").value; if(''==filepath){ alert("请选择要上传的excle文件"); return; } document.location.href="${base}/system/operatorBatchManager.action?act=doOther&filePath="+filepath; }


action: /**

  • 文件的路径,页面中只要写name="filePath",这样后台就可以拿到值啦 / private String filePath; /*
  • 文件 */ private File myFile;

// myFileContentType属性用来封装上传文件的类型 private String myFileContentType;

// myFileFileName属性用来封装上传文件的文件名 private String myFileFileName;

private String fileName;// 初始的通过param指定的文件名属性

private String inputPath;// 指定要被下载的文件路径 //以上get,set略

//以下是文件下载方法 public String execute() throws Exception { if("download".equals(this.act)){ return SUCCESS; }else{ return super.execute(); } }

public InputStream getInputStream() throws Exception {

	// 文件下载目录路径
	String downloadDir = ServletActionContext.getServletContext()
			.getRealPath("/upload");
	// 文件下载路径
	String downloadFile = ServletActionContext.getServletContext()
			.getRealPath(inputPath);
	File file = new File(downloadDir);
	// 真实文件路径,去掉里面的..等信息
	downloadFile = file.getCanonicalPath();
	// 发现企图下载不在/upload下的文件,就显增空内容
	if (!downloadFile.startsWith(downloadDir)) {
		return null;
	}
	// 通过 ServletContext,也就是application 来读取数据
	return ServletActionContext.getServletContext().getResourceAsStream(
			inputPath);
}

/**
 * 提供转换编码后的供下载用的文件名
 */
public String getDownloadFileName() {
	String downFileName = fileName;
	try {
		downFileName = new String(downFileName.getBytes(), "GBK");
	} catch (Exception e) {
		e.printStackTrace();
	}
	return downFileName;
}

//以下是文件上传方法 public String doOther() throws Exception {

	myFile = new File(filePath);
	// 获得文件名
	String type = myFile.getName();// this.getMyFileFileName();
	int i = 0;
	while (i != -1) {
		// 找到上传文件的类型的位置,这个地方的是'.'
		i = type.indexOf(".");
		// 截取上传文件的后缀名,此时得到了文件的类型
		type = type.substring(i + 1);
	}
	// 如果是excle文件就开始处理
	if ("xls".equals(type) || "xlsx".equals(type)) {
		// 获得文件路径
		String str_path = myFile.toString();
		this.excelDataImportOracle(str_path);
	} else {
		// 文件类型不对
		this.setErrorInfo(getText("info.fileErrorType"));
	}
	return "addInput";
}

public void excelDataImportOracle(String pathname) throws Exception { try {

		/** 上传文件到服务器 */
		// 得到文件存放的目录路径
		String dir = ServletActionContext.getServletContext().getRealPath(
				"/upload");
		String fileName = myFile.getName();
		String targetFileName = generateFileName(fileName);

		InputStream is = new FileInputStream(myFile);
		// 设置目标文件
		File toFile = new File(dir, targetFileName);
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(toFile));

		byte b = new byte;
		int length = 0;
		while ((length = is.read(b)) != -1) {
			bos.write(b, 0, length);
			bos.flush();
		}
		bos.close();
		is.close();
	} catch (Exception e) {
		e.printStackTrace();
	}

struts.xml配置文件:

	<action name="operatorBatchManager" class="com.tempus.fw.operatorBatchAction">
		<result name="success" type="redirect">
			operatorSearch.action?act=doquery&amp;busiId=searchOperator
		</result>
		<result name="addInput" type="freemarker">
			/WEB-INF/core/pages/system/operatorBatchOpt.ftl
		</result>
	</action>
	<action name="batchDomnManager" class="com.tempus.fw.operatorBatchAction">
		<param name="inputPath">/upload/mould.xls</param>
		<!-- 初始文件名 -->
		<param name="fileName">mould.xls</param>
		<!-- 下載配置 -->  
		<result name="success" type="stream">  
			<!-- 下載類型和編碼配置 -->  
			<param name="contentType">application/octet-stream;charset=GBK</param>     
			<!-- action中提供输入流的属性名称,缺省为inputStream  -->  
			<param name="inputName">inputStream</param>  
			<!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性 对应action类中的方法 getDownloadFileName() -->    
			<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>    
			<!-- 緩存 -->  
			<param name="bufferSize">4096</param>  
		</result>
	</action>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。