jspsmartupload 乱码问题和文件上传下载

最后乱码空指针问题

表单中提交中text内容request.getParameter("")获取为空

解决方法:SmartUpload su = new SmartUplad();

su.getRequest.getParameter("");如果页面编码格式为utf-8,但这种获得结果中文乱码 ,如果页面编码格式为gbk,获取不乱码。页面编码UTF-8,已下方法解决中文乱码, new String(su.getRequest.getParameter("").getBytes(),"UTF-8");



文件上传下载

1.jsp页面代码

test.jsp:

<form action="<%=path%>/TestFileUpload.do" enctype="multipart/form-data" method="post">
  		<input type="file" name="file1" size="30" />
  		<input type="file" name="file2" />
  		<input type="file" name="file3"/>
  		<input type="submit" value="上传"/>
  </form>
 <a href="<%=path %>/TestFileDownload.do?fileName=test.txt" > 下载文件test.txt</a>

2.servlet代码

TestFileUpload文件上传servlet类中doPost方法代码:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 上传服务器下文件路径
		String filePath = "D:/weixinimages/nginx/html";
		Writer out = resp.getWriter();
		if (FileUploadUtil.jspSmartUploadFile(this.getServletConfig(), req,
				resp, filePath)) {
			out.write("<h>上传成功!!!</h>");
		} else {
			out.write("<h>上传失败!!!</h>");
		}
		out.flush();
		out.close();
	}

TestFileDownload文件下载servelt中doGet方法代码:

                String fileName = req.getParameter("fileName");
		// 下载文件路径 
		String filePath = "D:/weixinimages/nginx/html/" + fileName;
		// 上传结果
		String result = "";
		if (FileUploadUtil.jspSmartDownloadFile(
				this.getServletConfig(), req,resp, filePath)) {
			result = "---上传成功";
		} else {
			result = "---上传失败";
		}
		req.setAttribute("result", result);

两个servlet中调用了FileUploadUtil工具类中两个方法代码:

/**
	 * 
	 *(描述:) jspSmartUpload 上传文件  form表单 enctype="multipart/form-data"
	 * @method jspSmartUpload
	 * @param sc
	 * @param req
	 * @param resp
	 * @param filePath
	 * void
	 */
	public static boolean jspSmartUploadFile (ServletConfig sc,
			HttpServletRequest req, HttpServletResponse resp ,String filePath){
				    boolean flag  = true;
				// 创建文件路径 如:D:/weixinimages/nginx/html  
				File file = new File(filePath);
				if(!file.exists()){
					file.mkdir();
				}
				try{    
					// 初始化对象
					su.initialize(sc, req, resp);
					// 设置上传文件大小不为5M
					su.setMaxFileSize(1024*1024*5);
					// SmartUpload 为多文件上传下载处理第三方组件
					// 设置所有文件上传最大大小 50M
					su.setTotalMaxFileSize(1024*1024*50);
					//设置允许上传文件类型
					su.setAllowedFilesList("jpg,png,gif");
					// 拒绝上传文件内容
					su.setDeniedFilesList("rar,jsp,js,css");
					
					su.upload();
					// su.save 返回类型为int 为保存文件数
					int cout = su.save(filePath);
					logger.info("成功上传" + cout+"张图片!!!");
					
				}catch(Exception e){
					e.printStackTrace();
					flag = false;
				}	
				return flag;
	}
	/**
	 * 
	 *(描述:) jspsmartupload 下载文件
	 * @method jspSmartDownloadFile
	 * @param sc
	 * @param req
	 * @param resp
	 * @param filePath
	 * @return
	 * boolean
	 */
	public static boolean jspSmartDownloadFile(ServletConfig sc,
			HttpServletRequest req, HttpServletResponse resp ,String filePath){
		    boolean flag = true;
		    try {
			// 初始化
			su.initialize(sc, req, resp);
			// 设置contentDisposition为null 禁止浏览器打开文件
			su.setContentDisposition(null);
			su.downloadFile(filePath);
			
		    } catch(Exception e) {
			e.printStackTrace();
			flag = false;
		    }
		    return flag;
	}

最后附上下载文件效果图:




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值