使用smartupload.jar实现文件上传
1.将jar包添加到项目中:smartupload.jar
2.准备上传的页面
<form action="toUpload" method="post" enctype="multipart/form-data" > 书名:<input type="text" name="bookName"/><br> 图片:<input type="file" name="自定义名称"/><br> <input type="submit" value="提交"/> </form>
注:(1)form标签中要添加enctype属性
(2)提交方式必须是post
3.开始获取数据,保存文件
实例代码:
public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //上传文件 SmartUpload su=new SmartUpload(); //获得pageContext对象 JspFactory factory=JspFactory.getDefaultFactory(); PageContext pagecontext= factory. getPageContext(this, request,response, null,false,1024,true); su.initialize(pagecontext); su.setCharset("utf-8"); //实现文件数据的上传 su.upload(); File file = su.getFiles().getFile(0); //得到文件的基本信息 String filename=file.getFileName(); String type=file.getContentType(); System.out.println("type="+type); String url="uploadfile/"+filename; //将上传文件保存到指定目录 file.saveAs(url, SmartUpload.SAVE_VIRTUAL); request.setAttribute("filename",filename); String uname=su.getRequest().getParameter("uname"); System.out.println("uname="+uname); request.getRequestDispatcher("success.jsp").forward(request, response); } catch (SmartUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
注:(1)此时如果表单中有其他数据时,不能通过request直接获取,需要通过SmartUpload对象获取
String name=su.getRequest().getParameter("bookName");
并且该代码要在SmartUpload操作完成后添加
(2)解决乱码:
new String(name.getBytes("GBK"),"utf-8")
注:斜杠方向:/
注意:
smartupload常用方法