java问题 enctype="multipart/form-data"
....response.setCharacterEncoding("utf-8");response.setHeader("Content-type","text/ht...
....
response.setCharacterEncoding("utf-8");
response.setHeader("Content-type", "text/html;charset=utf-8");
PrintWriter out = response.getWriter();
S_shoppingService service = new S_shoppingService();
S_shopping s = WebshopUtils.request2Bean(request,S_shopping.class);
String sid = s.getSid();
boolean b = service.insert(s);
String msg = null;
if(b)
{
S_imageService service1 = new S_imageService();
S_image i = WebshopUtils.request2ImgBean(request, S_image.class);
i.setIsid(sid);
boolean c = service1.insert(i);
if(c)
msg = "添加图书成功";
else
msg = "添加图书失败";
}else{
msg="添加图书失败";
}
request.setAttribute("msg", msg);
request.getRequestDispatcher("/servlet/AddShoppingServlet").forward(request, response);
}
public static<T> T request2Bean(HttpServletRequest request,
Class<T> class1) {
try {
T bean = class1.newInstance();
String sid = request.getParameter("sid");
if(sid == null)
{
sid = UUID.randomUUID().toString();
BeanUtils.setProperty(bean, "sid", sid);
}
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
if(!upload.isMultipartContent(request))
throw new WebException("不是表单提交过来的数据");
List<FileItem> items = upload.parseRequest(request);
for(FileItem item:items)
{
if(item.isFormField())
{
String name = item.getFieldName();
String value = item.getString("utf-8");
if("sid".equals(name))
sid = value;
ConvertUtils.register(new DateLocaleConverter(), Date.class);
BeanUtils.setProperty(bean, name, value);
}else{
String filename = item.getName();
//获得文件上传的扩展名.jpg .bmp .png
String ExeName = filename.substring(filename.lastIndexOf(".")+1);
if(!ExeName.endsWith("jpg"))
throw new WebException("请上传正确的图片");
String img = sid+"."+ExeName;
String dir = request.getSession().getServletContext().getRealPath("/upload");
File file = new File(dir,img);
file.createNewFile();
InputStream in = item.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
Streams.copy(in, fos, true);
BeanUtils.setProperty(bean, "img", img);
item.delete();
}
}
return bean;
} catch (Exception e) {
throw new WebException("WebshopUtils出现异常");
}
}
同上写两遍,只是字段不一样!请问第一个upload.parseRequest(request);能取过值,第二个为什么就取不上值呢?
我的数据库是两个数据库一个是数据,一个图片而且是有外键的 展开
response.setCharacterEncoding("utf-8");
response.setHeader("Content-type", "text/html;charset=utf-8");
PrintWriter out = response.getWriter();
S_shoppingService service = new S_shoppingService();
S_shopping s = WebshopUtils.request2Bean(request,S_shopping.class);
String sid = s.getSid();
boolean b = service.insert(s);
String msg = null;
if(b)
{
S_imageService service1 = new S_imageService();
S_image i = WebshopUtils.request2ImgBean(request, S_image.class);
i.setIsid(sid);
boolean c = service1.insert(i);
if(c)
msg = "添加图书成功";
else
msg = "添加图书失败";
}else{
msg="添加图书失败";
}
request.setAttribute("msg", msg);
request.getRequestDispatcher("/servlet/AddShoppingServlet").forward(request, response);
}
public static<T> T request2Bean(HttpServletRequest request,
Class<T> class1) {
try {
T bean = class1.newInstance();
String sid = request.getParameter("sid");
if(sid == null)
{
sid = UUID.randomUUID().toString();
BeanUtils.setProperty(bean, "sid", sid);
}
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
if(!upload.isMultipartContent(request))
throw new WebException("不是表单提交过来的数据");
List<FileItem> items = upload.parseRequest(request);
for(FileItem item:items)
{
if(item.isFormField())
{
String name = item.getFieldName();
String value = item.getString("utf-8");
if("sid".equals(name))
sid = value;
ConvertUtils.register(new DateLocaleConverter(), Date.class);
BeanUtils.setProperty(bean, name, value);
}else{
String filename = item.getName();
//获得文件上传的扩展名.jpg .bmp .png
String ExeName = filename.substring(filename.lastIndexOf(".")+1);
if(!ExeName.endsWith("jpg"))
throw new WebException("请上传正确的图片");
String img = sid+"."+ExeName;
String dir = request.getSession().getServletContext().getRealPath("/upload");
File file = new File(dir,img);
file.createNewFile();
InputStream in = item.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
Streams.copy(in, fos, true);
BeanUtils.setProperty(bean, "img", img);
item.delete();
}
}
return bean;
} catch (Exception e) {
throw new WebException("WebshopUtils出现异常");
}
}
同上写两遍,只是字段不一样!请问第一个upload.parseRequest(request);能取过值,第二个为什么就取不上值呢?
我的数据库是两个数据库一个是数据,一个图片而且是有外键的 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询