java jsp 怎么上传图片到servlet
2个回答
展开全部
jsp:
<form action="up.do" method="post" enctype="multipart/form-data" id="upform"> 文件:<input type="file" name="file" id="file"/> <input type="button" id="btn" value="上传"/> </form>
SERVLET:
FileItemFactory fileItemFactory = new DiskFileItemFactory(); ServletFileUpload fileUpload = new ServletFileUpload(fileItemFactory); //设置 中文 编码集 fileUpload.setHeaderEncoding("UTF-8"); try { List<FileItem> fileItems= fileUpload.parseRequest(request); for (FileItem fi : fileItems) { if(fi.isFormField()){ } } } for (FileItem fileItem : fileItems) { if(fileItem.isFormField()){ }else{ //文件名 String filename = fileItem.getName(); //文件名处理 if(filename.indexOf('\\') != -1){ filename = filename.substring(filename.lastIndexOf('\\')+1); } System.out.println(filename); if(filename == null || filename.equals("")){ request.setAttribute("msg", "没有指定上传文件"); request.getRequestDispatcher("index.jsp").forward(request, response); //验证码错误 就退出 return; } //path 生成文件夹 String fpath = "F:\\\\中软高科\\\\小组\\\\5组日报"+UpFileUtil.getMonthadnDate(); File file = new File(fpath); if(!file.exists()){ file.mkdir(); } //文件全路径 fpath = fpath+"\\\\"+filename;// System.out.println(filename+"--->"+fpath+"--->"+UpFileUtil.getTime()); //写出文件到 服务器磁盘 InputStream in = fileItem.getInputStream();//request.getInputStream(); OutputStream out = new FileOutputStream(fpath); byte[] buff = new byte[1024*10]; while(in.read(buff) != -1){ out.write(buff); } out.flush(); out.close(); in.close(); //保存文件信息到数据库 new UpFileDao().updFile("{call file_add(?,?,?)}",new String[]{filename,fpath,UpFileUtil.getTime()}); filename = null; fpath = null; file = null; request.setAttribute("msg", "上传成功"); request.getRequestDispatcher("index.jsp").forward(request, response); } } } catch (Exception e) { e.printStackTrace(); }
<form action="up.do" method="post" enctype="multipart/form-data" id="upform"> 文件:<input type="file" name="file" id="file"/> <input type="button" id="btn" value="上传"/> </form>
SERVLET:
FileItemFactory fileItemFactory = new DiskFileItemFactory(); ServletFileUpload fileUpload = new ServletFileUpload(fileItemFactory); //设置 中文 编码集 fileUpload.setHeaderEncoding("UTF-8"); try { List<FileItem> fileItems= fileUpload.parseRequest(request); for (FileItem fi : fileItems) { if(fi.isFormField()){ } } } for (FileItem fileItem : fileItems) { if(fileItem.isFormField()){ }else{ //文件名 String filename = fileItem.getName(); //文件名处理 if(filename.indexOf('\\') != -1){ filename = filename.substring(filename.lastIndexOf('\\')+1); } System.out.println(filename); if(filename == null || filename.equals("")){ request.setAttribute("msg", "没有指定上传文件"); request.getRequestDispatcher("index.jsp").forward(request, response); //验证码错误 就退出 return; } //path 生成文件夹 String fpath = "F:\\\\中软高科\\\\小组\\\\5组日报"+UpFileUtil.getMonthadnDate(); File file = new File(fpath); if(!file.exists()){ file.mkdir(); } //文件全路径 fpath = fpath+"\\\\"+filename;// System.out.println(filename+"--->"+fpath+"--->"+UpFileUtil.getTime()); //写出文件到 服务器磁盘 InputStream in = fileItem.getInputStream();//request.getInputStream(); OutputStream out = new FileOutputStream(fpath); byte[] buff = new byte[1024*10]; while(in.read(buff) != -1){ out.write(buff); } out.flush(); out.close(); in.close(); //保存文件信息到数据库 new UpFileDao().updFile("{call file_add(?,?,?)}",new String[]{filename,fpath,UpFileUtil.getTime()}); filename = null; fpath = null; file = null; request.setAttribute("msg", "上传成功"); request.getRequestDispatcher("index.jsp").forward(request, response); } } } catch (Exception e) { e.printStackTrace(); }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询