struts2 多文件上传与下载

 List<File> upload;
	//上传文件名集合   
private List<String>  uploadFileName;//上传的文件的自带名有后缀
private InputStream inputStream; // 下载用的
List<Fileform> addlist4;// 接收项目文件

public List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

public List<File> getUpload() {
		return upload;
	}

	public void setUpload(List<File> upload) {
		this.upload = upload;
	}
public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}


文件上传的方法:
if (addlist4!=null) {
				int j=0;
				for (int i = 0; i < addlist4.size(); i++) {
					Fileform file = addlist4.get(i);
					if(file!=null){
					String path = saveUploadFile(upload.get(j++));
					String filename=this.getUploadFileName().get(i);
					file.setFilename(filename);
					file.setPath(path);
					file.setOtherformid(project.getProjectid());
					file.setFileforename(file.getFileforename());
					file.setDeletestate(0);
					fileformService.save(file);
					}
				}
			}


/**
	 * 保存上传的文件,并返回文件在服务端的真实存储路径
	 * 
	 * @param upload
	 * @return
	 */
	protected String saveUploadFile(File upload) {
		SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
		// >> 获取路径
		String basePath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload_files");
		String subPath = sdf.format(new Date());
		// >> 如果文件夹不存在,就创建
		File dir = new File(basePath + subPath);
		if (!dir.exists()) {
			dir.mkdirs(); // 递归的创建不存在的文件夹
		}
		// >> 拼接路径
		String path = basePath + subPath + UUID.randomUUID().toString();
		// >> 移动文件
		upload.renameTo(new File(path)); // 如果目标文件夹不存在,或是目标文件已存在,就会不成功,返回false,但不报错。
		return path;
	}


下载的方法:
	/** 下载 */
	public String download() throws Exception {
		// 准备下载的资源
		Fileform fileform = fileformService.getById(model.getFileid());
		inputStream = new FileInputStream(fileform.getPath());
		// 准备文件名(解决乱码问题)
		String fileName = URLEncoder.encode(fileform.getFilename(), "utf-8"); // 方法一
		ActionContext.getContext().put("fileName", fileName);
		return "download";
	}

jsp:页面:

<script type="text/javascript">
var num4=0;
function addfile(){
    	var tr = document.createElement('tr');
		var td = document.createElement('td');
		
         td.innerHTML = "<td ><input type='text' name='addlist4["+num4+"].fileforename' required='required' id='filename'></td>";
		 tr.appendChild(td);
		 td = document.createElement('td');
		 td.innerHTML = "<td > <input type='file' name='upload'  value='浏览'  style='width:200px;'></td>";
		 tr.appendChild(td);
		 td = document.createElement('td');
		 td.innerHTML = "<td  ><textarea rows='2' cols='20' required='required' name='addlist4["+num4+"].filedesc'></textarea></td>";
		 tr.appendChild(td);
		 td = document.createElement('td');
		 td.innerHTML = "<td> <input type='button' value='删除' οnclick='removeSelect4(this)'/></td>";
		 tr.appendChild(td);
		 var dct = document.getElementById("table4");
		 dct.tBodies[0].appendChild(tr);
		 ++num4;
            }
			function removeSelect4(inputobj){
     var parentTD = inputobj.parentNode;  
     var parentTR = parentTD.parentNode;  
     var parentTBODY = parentTR.parentNode;  
       parentTBODY.removeChild(parentTR);
		}
</script>



struts2的配置:
在特定的action中
<!-- 下载专用的结果配置 -->
			<result name="download" type="stream">
				<param name="contentType">application/octet-stream</param>
				<param name="inputName">inputStream</param>
				<param name="contentDisposition">attachment;filename="${#fileName}.doc"</param>
			</result>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值