javaweb上传文件(sevlet)

3 篇文章 0 订阅
把file标签与其他标签分开只处理file标签并把其他参数重定向到另外的函数或servlet


public class D_BookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private D_BookService d_BookService = new D_BookServiceImpl();

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getParameter("method");
System.out.println(method);
if ("add".equals(method)) {
upLoadFile(request, response);
}else if("addbook".equals(method)){
addBook(request, response);
}else if("edit".equals(method)){
showBook(request, response);
}

}
//添加图书
private void addBook(HttpServletRequest request, HttpServletResponse response) throws IOException{
D_Book d_Book = new D_Book();
String product_pic = request.getParameter("product_pic");
String product_name = request.getParameter("product_name"); // varchar(100) NOT NULL,-- 商品名称
String fixed_price = request.getParameter("fixed_price"); // double NOT NULL,-- 商品定价
String dang_price = request.getParameter("dang_price"); //double NOT NULL,-- 当当价
String author = request.getParameter("author") ; //varchar(200) NOT NULL,-- 作者
String publishing = request.getParameter("publishing"); //varchar(200) NOT NULL,-- 出版社
String publish_time = request.getParameter("publish_time"); //date NOT NULL,-- 出版时间
String author_summary= request.getParameter("author_summary"); // varchar(100) NOT NULL,-- 作者简介
String description = request.getParameter("description"); //varchar(100) default NULL,-- 商品描述
d_Book.setProduct_pic(product_pic);
d_Book.setProduct_name(product_name);
d_Book.setDang_price(Double.parseDouble(dang_price));
d_Book.setFixed_price(Double.parseDouble(fixed_price));
d_Book.setAuthor(author);
d_Book.setPublishing(publishing);
d_Book.setPublish_time(Date.valueOf(publish_time));
d_Book.setAuthor_summary(author_summary);
d_Book.setDescription(description);
d_Book.setScore(1);
d_BookService.insertD_Book(d_Book);
response.sendRedirect("/DangDang/Admin/jspfile/editbook.jsp");
}
//上传文件
@SuppressWarnings("unchecked")
public void upLoadFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// 在解析请求之前先判断请求类型是否为文件上传类型
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
String url = "";
if(isMultipart){
// 文件上传处理工厂
FileItemFactory fileItemFactory = new DiskFileItemFactory();
// 创建文件上传处理器
ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
// 开始解析请求信息
try {
List<FileItem> fileItems = servletFileUpload.parseRequest(request);
// 对所有请求信息进行判断
for(FileItem fileItem: fileItems){
//如果提交信息为表单格式设置为新的url
if (fileItem.isFormField()) {
String fieldName = fileItem.getFieldName();
String value = fileItem.getString();
url += "&" + fieldName + "=" + value;
}
//如果提交信息为文件格式
else {
String fileName = fileItem.getName();
String basePath = System.getProperty("user.dir").replace("\\", "/") + "/../webapps/DangDang/BookImage/";
File file = new File(basePath, fileName);
if (!file.exists()) {
file.createNewFile();
}
fileItem.write(file);
url += "&product_pic" + "=" + "BookImage/" + fileName;
}
}
} catch (FileUploadException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
//把设置好的url重新转发到servlet或函数
response.sendRedirect("/DangDang/admindbook?method=addbook" + url);
}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值