本文主要是介绍使用commons-fileupload和commons-io上传文件和接收字段和参数(解决参数乱码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
之前遇到用commons-fileupload和commons-io 这两个包来接收表单的数据和文件 就出现各种乱码和接收不到参数的问题,最终参考了各种资料写出下面的可行的模板 这是注册商铺的流程大家可以用自己的业务去替换,
表单一定要要设置 method=“post” enctype=“multipart/form-data”
接收参数的时候可以用map来封装数据我直接写死了用switch来实现了,
前台:
<form id="shopform" onsubmit="return false;" action="regshop" method="post" enctype="multipart/form-data"><h1>商铺注册</h1><br><div class="form-group"><h4>店铺名</h4><input name="shop_name" type="text" class="form-control" id="exampleInputEmail1" placeholder="请输入要注册的店铺名"></div><div class="form-group"><h4>店铺地址</h4><input name="shop_address" type="text" class="form-control" id="exampleInputPassword1" placeholder="请输入你注册的店铺地址"></div><div class="form-group"><h4>店铺简介</h4><input name="shop_value" type="text" class="form-control" id="exampleInputadress" placeholder="简介"></div><div class="form-group"><label for="inputfile" style="font-size: 20px">上传图片</label><input type="file" id="inputfile" name="img"><label style="font-size: 15px;padding-left: 100px">图片阅览</label><div id="imagg" style="height: 100px;width: 50px;margin-left: 100px;"></div></div><button type="submit" class="btn btn-default">注册</button></form>
后台:
response.setHeader("Pragma", "No-cache");response.setDateHeader("Expires", 0);response.setHeader("Cache-Control", "no-cache");JSONObject obj = new JSONObject();Shop shop1=null;String shop_name=null;String shop_address=null;String shop_value=null;String suffix=null;PrintWriter out= response.getWriter();//判断请求是否为multipart请求if(!ServletFileUpload.isMultipartContent(request)){throw new RuntimeException("当前请求不支持文件上传");}try {//创建一个FileItem工厂DiskFileItemFactory factory = new DiskFileItemFactory();//创建文件上传核心组件ServletFileUpload servletFileUpload = new ServletFileUpload(factory);//解析请求,获取到所有的itemList<FileItem> items = servletFileUpload.parseRequest(request);//遍历itemsfor(FileItem item : items){if(item.isFormField()){ //若item为普通表单项String fieldName = item.getFieldName(); //获取表单项名称String fieldValue = item.getString(); //获取表单项的值fieldValue= new String(fieldValue.getBytes("ISO-8859-1"),"utf-8");// new String(s.getBytes("ISO-8859-1"),"utf-8"); 转码 //这里就是解决乱码问题的switch (fieldName) {case "shop_name":shop_name = fieldValue;shop1 = shopimpl.selectshopname(shop_name);if (shop1!=null){response.setStatus(300);obj.put("status", 300);obj.put("message", Const.HADSHOPNAME);}break;case "shop_address":shop_address=fieldValue;break;case "shop_value":shop_value=fieldValue;break;}System.out.println(fieldName+"="+fieldValue);}else{ //若item为文件表单项//获取上传文件原始名称String fileName = item.getName();suffix="."+fileName.substring(fileName.lastIndexOf(".") + 1);//获取输入流,其中有上传 文件的内容InputStream is = item.getInputStream();//获取文件保存在服务器的路径String path = this.getServletContext().getRealPath("/images/shop");//创建目标文件,将用来保存上传文件File file = new File(path, shop_name+suffix);//将输入流中的数据写入到输出流中OutputStream os = new FileOutputStream(file);//将输入流中的数据写入输出流中int len = -1;byte[] buf = new byte[1024];while((len = is.read(buf)) != -1){os.write(buf, 0, len);}os.close();is.close();}}} catch (FileUploadException e) {e.printStackTrace();}if (shop1==null){Shop shop = new Shop();shop.setShop_name(shop_name);//设置店铺名字shop.setShop_address(shop_address);//设置店铺地址shop.setShop_value(shop_value);//设置店铺简介shop.setShop_img_url("images/shop/"+shop_name+suffix);//存放上传店铺图片的相对路径shop.setScore(9.0f);//默认9.0评分shopimpl.insert(shop);response.setStatus(200);obj.put("status", 200);obj.put("message", "店铺插入成功");}out.println(JSONObject.toJSON(obj));out.flush();out.close();
这篇关于使用commons-fileupload和commons-io上传文件和接收字段和参数(解决参数乱码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!