上传图片
private void uploadImage(ProductInfoForm piForm, String physicsPath,String name) {
FormFile file = piForm.getUpLoadImage();
FileOutputStream fos = null;
try {
byte[] temp = file.getFileData();
File image = new File(physicsPath,name);
fos = new FileOutputStream(image);
fos.write(temp);
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
添加产品
public ActionForward addProductInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ProductInfoForm piForm = (ProductInfoForm)form;
String name = (String)piForm.getName();
String waybillNo = (String)piForm.getWaybillNo();
Float price = (Float)piForm.getPrice();
Integer num = (Integer)piForm.getNum();
Integer type = (Integer)piForm.getType();
String description = (String)piForm.getDescription();
//String[] imagesType = {".gif", ".jpg",".pnf" };
Integer tuijianid = (Integer)piForm.getTuijianid();
Integer hotid = (Integer)piForm.getHotid();
Integer accountid = (Integer)piForm.getAccountid();
List list = (List)productInfoService.findByName(name);
request.setAttribute("nameerror", "商品已存在!");
if(list.size() > 0){
return mapping.findForward("add");
}
List list2 = (List)productInfoService.findByWaybillNo(waybillNo);
request.setAttribute("waybillNoerror", "货单号已存在!");
if(list2.size() > 0){
return mapping.findForward("add");
}
if(tuijianid == null){
tuijianid = 0;
}
if(hotid == null){
hotid = 0;
}
ProductInfo productInfo = new ProductInfo();
String physicsPath = request.getSession().getServletContext().getRealPath("/")+ "upload\\productimages\\";
FormFile file = piForm.getUpLoadImage();
String filename = file.getFileName();
int i = filename.lastIndexOf("\\");
int len = filename.length();
filename = filename.substring(len,i+1);
this.uploadImage(piForm, physicsPath,filename);
productInfo.setPicture("upload/productimages"+filename);
productInfo.setName(name);
productInfo.setWaybillNo(waybillNo);
productInfo.setPrice(price);
productInfo.setNum(num);
productInfo.setDescription(description);
Producttype producttype = new Producttype();
producttype.setId(type);
productInfo.setType(producttype);
productInfo.setTuijianid(tuijianid);
productInfo.setHotid(hotid);
User user = new User();
user.setId(accountid);
productInfo.setAccountid(user);
productInfo.setAdd_time(new Date());
productInfoService.save(productInfo);
request.setAttribute("productInfo", productInfo);
return this.getPageData(mapping, form, request, response);
}
错误信息:
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: BeanUtils.populate
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:495)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:816)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
com.shop.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:22)
root cause java.lang.IllegalArgumentException: Cannot invoke com.shop.back.form.ProductInfoForm.setUpLoadImage - argument type mismatch
org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1778)
org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1759)
org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648)
org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:1677)
org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1022)
org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811)
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:493)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:816)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
com.shop.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:22)
private void uploadImage(ProductInfoForm piForm, String physicsPath,String name) {
FormFile file = piForm.getUpLoadImage();
FileOutputStream fos = null;
try {
byte[] temp = file.getFileData();
File image = new File(physicsPath,name);
fos = new FileOutputStream(image);
fos.write(temp);
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
添加产品
public ActionForward addProductInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ProductInfoForm piForm = (ProductInfoForm)form;
String name = (String)piForm.getName();
String waybillNo = (String)piForm.getWaybillNo();
Float price = (Float)piForm.getPrice();
Integer num = (Integer)piForm.getNum();
Integer type = (Integer)piForm.getType();
String description = (String)piForm.getDescription();
//String[] imagesType = {".gif", ".jpg",".pnf" };
Integer tuijianid = (Integer)piForm.getTuijianid();
Integer hotid = (Integer)piForm.getHotid();
Integer accountid = (Integer)piForm.getAccountid();
List list = (List)productInfoService.findByName(name);
request.setAttribute("nameerror", "商品已存在!");
if(list.size() > 0){
return mapping.findForward("add");
}
List list2 = (List)productInfoService.findByWaybillNo(waybillNo);
request.setAttribute("waybillNoerror", "货单号已存在!");
if(list2.size() > 0){
return mapping.findForward("add");
}
if(tuijianid == null){
tuijianid = 0;
}
if(hotid == null){
hotid = 0;
}
ProductInfo productInfo = new ProductInfo();
String physicsPath = request.getSession().getServletContext().getRealPath("/")+ "upload\\productimages\\";
FormFile file = piForm.getUpLoadImage();
String filename = file.getFileName();
int i = filename.lastIndexOf("\\");
int len = filename.length();
filename = filename.substring(len,i+1);
this.uploadImage(piForm, physicsPath,filename);
productInfo.setPicture("upload/productimages"+filename);
productInfo.setName(name);
productInfo.setWaybillNo(waybillNo);
productInfo.setPrice(price);
productInfo.setNum(num);
productInfo.setDescription(description);
Producttype producttype = new Producttype();
producttype.setId(type);
productInfo.setType(producttype);
productInfo.setTuijianid(tuijianid);
productInfo.setHotid(hotid);
User user = new User();
user.setId(accountid);
productInfo.setAccountid(user);
productInfo.setAdd_time(new Date());
productInfoService.save(productInfo);
request.setAttribute("productInfo", productInfo);
return this.getPageData(mapping, form, request, response);
}
错误信息:
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: BeanUtils.populate
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:495)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:816)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
com.shop.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:22)
root cause java.lang.IllegalArgumentException: Cannot invoke com.shop.back.form.ProductInfoForm.setUpLoadImage - argument type mismatch
org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1778)
org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1759)
org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648)
org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:1677)
org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1022)
org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811)
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:493)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:816)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
com.shop.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:22)
解决方案 »
- java.lang.UnsupportedOperationException: The user must supply a JDBC connection
- jsf datatable 可以绑定一个HashMap么??
- struts2实现文件下载,能不能获取客户端是否下载成功,比如说,用户并没有下载,点了"取消"
- 高分求助:在radiobutton里可以实现被选中就执行一个函数吗?
- 急求JSP教程免费视频下载!!!!
- JB内为什么“import oracle.jdbc.driver.*;”抱错
- 求助:ssh2 怎么做ajax操作啊,如下代码为什么运行部成功?
- 出售JSP主机
- 一个jsp 的builde 过程中的问题
- 很奇怪的错误,请大家帮我看看,附代码!!
- 表头固定的问题,求解决方案,请帮忙解决...
- Oracle和javaWeb哪个先学哪个后学呀
public class ProductInfoForm extends ActionForm {
/**
* 2010年1月7号
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String waybillNo;
private Float price;
private Integer num;
private String description;
private Integer type;
private Integer tuijianid;
private Integer hotid;
private Integer accountid;
private Date add_time;
private FormFile upLoadImage;
//getter、setter方法
}
<%@ page language="java" pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>添加产品</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/003.css"> </head>
<body>
<center>
<form action="<c:url value='/back/sysProductInfo.do?method=addProductInfo&pageNo=1' />" method="post">
<table width="670px" cellpadding="0" cellspacing="0" border="0" class="table">
<tr>
<td colspan="3" class="style1">添加产品</td>
</tr>
<tr class="style2">
<td class="style3">*产品名称:</td>
<td class="style4"><input type="text" name="name" size="15"/></td>
<td class="style5">${nameerror }<div id="nameDiv"></div></td>
</tr>
<tr class="style2">
<td class="style3">*货单号:</td>
<td class="style4"><input type="text" name="waybillNo" size="15"/></td>
<td class="style5">${waybillNoerror }<div id="waybillNoDiv"></div></td>
</tr>
<tr class="style2">
<td class="style3">*价格:</td>
<td class="style4"><input type="text" name="price" size="10"/></td>
<td class="style5"><div id="priceDiv"></div></td>
</tr>
<tr class="style2">
<td class="style3">*数量:</td>
<td class="style4"><input type="text" name="num" size="10"/></td>
<td class="style5"><div id="numDiv"></div></td>
</tr>
<tr class="style2">
<td class="style3">*产品类型:</td>
<td colspan="2">
<select name="type">
<option value="">请选择...</option>
<option value="1">簿、本、册、卡</option>
<option value="2">书写用品</option>
<option value="3">收录用品</option>
<option value="4">桌面办公用品</option>
<option value="5">财务用品</option>
<option value="6">OA办公设备</option>
<option value="7">办公耗材</option>
<option value="8">电脑及数码产品</option>
<option value="9">美术用品</option>
<option value="10">测绘用品</option>
<option value="11">光学及摄像</option>
<option value="12">教学设施及用具</option>
<option value="13">体育休闲用品</option>
<option value="14">配件、原料及器材</option>
<option value="15">工艺品</option>
<option value="16">礼品</option>
<option value="17">玩具</option>
<option value="18">饰品及摆件</option>
<option value="19">学生用品</option>
<option value="20">其它</option>
</select>
</td>
</tr>
<tr>
<td class="style3">*产品描述:</td>
<td><textarea name="description" class="style7"></textarea></td>
<td class="style5"><div id="descriptionDiv"></div></td>
</tr>
<tr>
<td class="style3">*产品图片:</td>
<td><input type="file" id="upLoadImage" name="upLoadImage" /></td>
<td class="style5"><div id="pictureDiv"></div></td>
</tr>
<tr>
<td class="style3"> </td>
<td colspan="2"><input type="checkbox" id="tuijianid" name="tuijianid" value="1" />推荐商品
<input type="checkbox" id="hotid" name="hotid" value="1" />热卖商品
</td>
</tr>
<tr>
<td class="style3">操作人:</td>
<td colspan="2">
<select name="accountid">
<option value="${user.id }">${user.account }</option>
</select>
</td>
</tr>
<tr class="style2">
<td width="100px"><br></td>
<td colspan="2"><input type="submit" name="submit" id="submit" value="确定"/>
<input type="reset" name="reset" value="重置" /></td>
</tr>
</table>
</form>
</center>
</body>
</html>
productInfo.setName(name);
productInfo.setWaybillNo(waybillNo);
productInfo.setPrice(price);
productInfo.setNum(num);
productInfo.setDescription(description);
Producttype producttype = new Producttype();
producttype.setId(type);
productInfo.setType(producttype);
productInfo.setTuijianid(tuijianid);
productInfo.setHotid(hotid);
User user = new User();
user.setId(accountid);
productInfo.setAccountid(user);
productInfo.setAdd_time(new Date());
productInfoService.save(productInfo);
java.lang.StringIndexOutOfBoundsException: String index out of range: -5
java.lang.String.substring(String.java:1937)
com.shop.back.action.ProductInfoAction.addProductInfo(ProductInfoAction.java:161)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
com.shop.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:22)