fileupload实现照片上传

本文介绍了一种使用Java实现Web端图片上传的方法,通过DiskFileUpload解析请求并获取图片文件,利用GenericDelegator进行数据持久化。文章详细展示了如何处理上传过程中的参数及图片数据。
package com.yksoft.hrplan.departdoc; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.DiskFileUpload; import org.apache.commons.fileupload.FileItem; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.entity.GenericDelegator; import org.ofbiz.entity.GenericValue; public class WebMedia { public static String uploadImage(HttpServletRequest request, HttpServletResponse response) { GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); DiskFileUpload fu= new DiskFileUpload();//否属于一个普通表单字段,还是属于一个文件表单字段,如果是普通表单字段则返回true,否则返回false List lst=null; try { lst=fu.parseRequest(request);// } catch (Exception e) { e.printStackTrace(); } FileItem fi=null; FileItem imageFi=null; String imageFileName=""; byte[] imageBytes=null; Map passedParams = new HashMap(); for(int i=0;i<lst.size();i++) {="" fi="(FileItem)" lst.get(i);="" string="" fieldname="fi.getFieldName();" if(fi.isformfield())="" isformfield方法用于判断fileitem类对象封装的数据是否属于一个普通表单字段,还是属于一个文件表单字段,如果是普通表单字段则返回true,否则返回false。="" {="" string="" fieldstr="fi.getString();" passedparams.put(fieldname,="" fieldstr);="" }else="" if(fieldname.startswith("imagedata"))="" {="" imagefi="fi;" imagebytes="imageFi.get();" imagefilename="imageFi.getName();" }="" }="" request.setattribute("mediatable",="" passedparams.get("mediatable"));="" request.setattribute("tablename",="" passedparams.get("tablename"));="" request.setattribute("fieldname",="" passedparams.get("fieldname"));="" request.setattribute("systemcode",="" passedparams.get("systemcode"));="" request.setattribute("rulvalue",="" passedparams.get("rulvalue"));="" request.setattribute("id",="" passedparams.get("id"));="" string="" mediatable="passedParams.get(&quot;mediaTable&quot;).toString();" passedparams.remove("mediatable");="" passedparams.remove("rulvalue");="" genericvalue="" value="delegator.makeValue(mediaTable," passedparams);="" 对图象的处理="" if="" (imagebytes="" !="null" &&="" imagebytes.length=""> 0) { String mimeType = getMimeTypeFromImageFileName(imageFileName); if (UtilValidate.isNotEmpty(mimeType)) { value.put("mimeTypeId", mimeType); value.setBytes("imageData", imageBytes); } else { request.setAttribute("_ERROR_MESSAGE_", "mimeType is empty."); return "error"; } } //对照片字段的值处理,修改信息表中的图片的字段为1 if (passedParams.get("fieldName").equals("A001000")) { //保存图片, try { delegator.createOrStore(value); } catch (Exception ex) { ex.printStackTrace(); return "error"; } } return "success"; } //从文件名得到文件类型,其中对jpg单独处理 public static String getMimeTypeFromImageFileName(String imageFileName) { String mimeType = null; if (UtilValidate.isEmpty(imageFileName)) return mimeType; int pos = imageFileName.lastIndexOf("."); if (pos < 0) return mimeType; String suffix = imageFileName.substring(pos + 1); String suffixLC = suffix.toLowerCase(); if (suffixLC.equals("jpg")) mimeType = "image/jpeg"; else mimeType = "image/" + suffixLC; return mimeType; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值