struts2中的表单提交与文件上传下载

做项目时用到了表单数据和文件要同时提交,但文件字节流传递到后台,和我提交的其他的属性值不一样,让我有些苦恼,后来找到了方法

一.jsp页面代码

 <form action="" method="post" enctype="multipart/form-data">
 价格:<input type="text" required="required" onkeyup="this.value=/^\d+\.?\d{0,2}$/.test(this.value) ? this.value : ''" placeholder="请输入数字"><br>
 <input type="file" name="file" class="file" accept="image/*" value="选择图片">//这里没有限制图片格式
    <div class="button">
      <input type="submit" value="添加">          
    </div>
</form>  

原来form表单中加入这个enctype="multipart/form-data"就可以同时提交表单和文件,因为表单数据提交会有对所有字符默认编码,(表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据)括号内为引用别人的解释

价格输入框中js是只能输入数字的限制

二.Action中的文件处理

1.文件上传

 

private List<File> file;	// 上传文件集合
private List<String> fileFileName;	// 上传文件名集合
private List<String> fileContentType;	// 上传文件内容类型集合
public String addMenu() throws Exception{
    for (int i = 0; i < file.size(); i++) {// 循环上传每个文件,这里省略了表单数据的处理。
                uploadFile(i);
            }
}

private void uploadFile(int i) throws FileNotFoundException, IOException {
		try {
			InputStream in = new FileInputStream(file.get(i));//得到文件集合的文件
			String dir = ServletActionContext.getRequest().getRealPath("uploadImg");//得到你想要保存文件的文件夹的路径
			File fileLocation = new File(dir);
			// 此处也可以在应用根目录手动建立目标上传目录
			if (!fileLocation.exists()) {//判断文件夹是否存在,不存在自动创建
				boolean isCreated = fileLocation.mkdir();
				if (!isCreated) {
					// 目标上传目录创建失败,可做其他处理,例如抛出自定义异常等,一般应该不会出现这种情况。
					return;
				}
			}
			String fileName = this.getFileFileName().get(i);//得到文件的名字
			File uploadFile = new File(dir, fileName);
			OutputStream out = new FileOutputStream(uploadFile);
			byte[] buffer = new byte[1024 * 1024];//限制了大小,可自行修改
			int length;
			while ((length = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}
			in.close();
			out.close();
		} catch (Exception ex) {
			System.out.println("上传失败!");
			ex.printStackTrace();
		}
	}

 

2.文件下载

 

public void downloadFile() throws FileNotFoundException, IOException{
             HttpServletResponse response = ServletActionContext.getResponse();
        HttpServletRequest quest = ServletActionContext.getRequest();
		//获得下载的文件名字
		String fileName = ;//这里一定要获得文件名字
		//解决get方式中文乱码
		//fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
	        //获得文件的绝对路径
		String realPath = request.getSession().getServletContext().getRealPath("uploadImg");
               //ServletActionContext.getRequest().getRealPath("uploadImg");//得到你想要保存文件的文件夹的路径               
               //创建文件对象
		File file = new File(realPath,fileName);
		if(!file.exists()){
			response.getWriter().write("文件不存在");
			return;
		}
		//response.setContentType("application/x-msdownload");
                //response.setContentType("application/octet-stream");
		//response.setHeader("Content-Disposition", "attachment;filename="+
                //new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
		response.addHeader("content-disposition","attachment;filename="+URLEncoder.encode(fileName,"utf-8"));
		IOUtils.copy(new FileInputStream(file),response.getOutputStream());
	}

这就是我写的文件上传于下载,若是我哪里写错了,或者理解有误,望大家评论指正

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值