频道栏目
首页 > 程序开发 > 综合编程 > 其他综合 > 正文
boolean isMultipart=ServletFileUpload.isMultipartContent(request);
2013-01-18 13:01:46      个评论      
收藏   我要投稿
package com.itheima.tfy.web.servlet;

 

 

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.List;

 

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

 

import org.apache.commons.beanutils.BeanUtils;

import org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

 

 

import com.itheima.tfy.domain.Book;

import com.itheima.tfy.domain.Category;

import com.itheima.tfy.serice.impl.BusinessServiceImpl;

import com.itheima.tfy.util.BookStoreLogger;

import com.itheima.tfy.util.IdGenertor;

 

 

public class ManagerServlet extends HttpServlet {

  private BusinessServiceImpl bs=new BusinessServiceImpl();

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

 

String operation=request.getParameter("operation");

if("addCategory".equals(operation)){

 

addCategory(request,response);

}

if("ShowAllCategory".equals(operation)){

ShowAllCategory(request,response);

}

if("addBookUI".equals(operation)){

addBookUI(request,response);

}

if("addBook".equals(operation)){

addBook(request,response);

}

 

 

}

//转向添加图书页面,主要目的是显示图书分类,便于选择

private void addBookUI(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

List<Category> cs=bs.findAllCategory();

BookStoreLogger.info("转向添加图书页面,显示图书分类,查询结果为:"+cs.size());

System.out.println("addBookUI");

request.setAttribute("cs", cs);

request.getRequestDispatcher("/manager/addBook.jsp").forward(request, response);

}

//添加图书,保存图书信息,上传图片

private void addBook(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

//保存上传路径

String storePath=getServletContext().getRealPath("/images");

try{

 

Book book=new Book();

DiskFileItemFactory factory=new DiskFileItemFactory();

ServletFileUpload upload=new ServletFileUpload(factory);

boolean isMultipart=ServletFileUpload.isMultipartContent(request);

 

if(isMultipart){

List<FileItem> items=upload.parseRequest(request);

for(FileItem item:items){

if(item.isFormField()){

//普通字段

String fileName=item.getName();

String fileValue=item.getString("UTF-8");

BeanUtils.setProperty(book, fileName, fileValue);

}else{

//上传字段

String fileName=item.getName();

fileName=fileName.substring(fileName.lastIndexOf("\\")+1);

fileName=IdGenertor.getId()+"_"+fileName;

 

String storeFile=storePath+"\\"+fileName;

 

InputStream in=item.getInputStream();

OutputStream out=new FileOutputStream(storeFile);

int len=-1;

byte [] b=new byte[1024];

while((len=in.read(b))!=-1){

out.write(b, 0, len);

}

in.close();

out.close();

item.delete();

book.setImage("/images"+fileName);

}

}

}

 

BookStoreLogger.info("将要保存的书的信息内容"+book);

bs.addBook(book);

request.setAttribute("message", "添加成功");

}catch(Exception e){

BookStoreLogger.error(e.getMessage());

request.setAttribute("message", "服务器忙");

}

request.getRequestDispatcher("/message.jsp").forward(request, response);

 

 

}

//查询显示所有的分类信息

private void ShowAllCategory(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

List<Category> cs=bs.findAllCategory();

 

BookStoreLogger.info("查询显示所有的分类信息,分类结果:"+cs.size());

request.setAttribute("cs",cs);

request.getRequestDispatcher("/manager/listCategory.jsp").forward(request, response);

 

}

//添加分类

private void addCategory(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException   {

try{

Category c=new Category();

BeanUtils.populate(c, request.getParameterMap());

BookStoreLogger.info("将要保存的分类内容"+c);

bs.addCategory(c);www.2cto.com

request.setAttribute("message", "添加成功");

}catch(Exception e){

BookStoreLogger.error(e.getMessage());

request.setAttribute("message", "服务器忙");

}

request.getRequestDispatcher("/message.jsp").forward(request, response);

}

 

 

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

 

 

doGet(request, response);

}

 

 

}

点击复制链接 与好友分享!回本站首页
相关TAG标签
上一篇:如何让页面一打开就远程自动下载文件
下一篇:cassandra的简单使用
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站