<form action="UploadServlet" enctype="multipart/form-data" method="post">
    		书籍名称:<input type="text"name="name"><br>
    		图片名称:上传文件:<input type="file" name="img"><br/>
    		<input type="submit"value="提交">
    		</form>

package web;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class UploadServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html");
		//实例化SmartUpload上传组件
		SmartUpload smart = new SmartUpload();
		//PageContext pageContext=JspFactory.getDefaultFactory().getPageContext(this, req, resp, null, true, 8192, true);
		//初始化上传操作
		smart.initialize(this.getServletConfig(),req,resp);
		smart.setMaxFileSize(1024*1024*10);
		smart.setAllowedFilesList("jpg,jpeg,bmp");
		//设置字符集
		smart.setCharset("gbk");
		//准备上传
		try {
			smart.setDeniedFilesList("exe,jsp,bat,html,,");
			smart.upload();
			Long time=new Date().getTime();
			String ext=smart.getFiles().getFile(0).getFileExt();//获取文件后缀
			String filename=time+"."+ext;
			//smart.getFiles().getFile(0).saveAs( "/img/ "+filename);//保存在容器目录下
			String pa=this.getServletContext().getRealPath("/")+"img"+java.io.File.separator+filename;
			System.out.println(pa);

			smart.getFiles().getFile(0).saveAs(pa);
			//			smart.save("img");
			Request smartReq  =smart.getRequest();
			String name=smartReq.getParameter("name");
			resp.getWriter().println(name);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//将上传的文件保存到项目根路径下的uploadfile目录中
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}
}

<Context path="/web" docBase="D:\updemo\WebRoot"/>

在tomcat的host目录下设置,Context子标签,让上传的时候,同步上传到项目目录下!!!

最新文章

  1. [解决方案]CREATE DATABASE statement not allowed within multi-statement transaction.
  2. Unity(四)IocContainer 封装类库
  3. MySQL 1045登录失败
  4. JavaScript学习笔记——对表单的操作
  5. windows的Timer和写文件方式串口注意!
  6. JSP 和 Servlet 有哪些相同点和不同点,他们之间的联系是什么?
  7. JavaScript 语句 for
  8. HTML5新事物
  9. C# 笛卡尔积
  10. 服务器调用JS
  11. 面试题_66_to_75_Java IO 和 NIO 的面试题
  12. Apache HTTP Server多个拒绝服务漏洞
  13. sass笔记-1|Sass是如何帮你又快又好地搞定CSS的
  14. spring 4 泛型注入
  15. Android杂谈--ListView之BaseAdapter的使用
  16. 1270: [BeijingWc2008]雷涛的小猫
  17. 17.QT-事件处理分析、事件过滤器、拖放事件
  18. 【代码总结● Swing中的一些操作与设置】
  19. Ubuntu系统常见问题
  20. 批量删除MSSQL 中主外键约束

热门文章

  1. nodeJs 对 Mysql 数据库的 curd
  2. stm32+lwip(五):以太网帧发送测试
  3. 解决 Python2 和 Python3 的共存问题
  4. SAP ABAP Development Tools in Eclipseのセットアップ
  5. 如何保证HashMap线程安全
  6. 插件开发遇到的坑------final 型变量,编译过程被优化
  7. Python全栈面试题
  8. 白话HMM系列1——从一个缩略语还原的例子说起
  9. cachel-control
  10. MySQL统计数据库表大小