通过Servlet实现页面上传文件(到硬盘F)
2016-08-31 10:37
323 查看
package servlet4; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.servlet.ServletException; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FileUpLoad extends HttpServlet { // 编码 private String encoding = "UTF-8"; // 解码 private String decoding = null; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); ServletInputStream sis = req.getInputStream(); int len = req.getContentLength(); int index = 0; String tmp = null; boolean isFirst = true; String firstLine = null; int[] iindex = new int[1]; byte[] bytes = new byte[4096]; String filename = null; while ((tmp = readLine(bytes, iindex, sis, encoding)) != null) { if (isFirst) { firstLine = tmp; isFirst = false; } index = tmp.indexOf("filename="); // 得到要上传文件的文件名 if (index != -1) { String tailString = tmp.substring(index + 10); if (tailString != null) { int ii = tailString.indexOf("\""); filename = tailString.substring(0, ii); } System.out.println(tmp); break; } } filename = getName(filename); if (filename == null) { filename = "file.out1"; } String filepath = "f:/" + filename; FileOutputStream fos = new FileOutputStream(filepath); // 定义文件上传结束标志 String endFlag = firstLine.substring(0, firstLine.length() - 2) + "--" + firstLine.substring(firstLine.length() - 2); String contentType = readLine(bytes, iindex, sis, encoding); if (contentType != null) { if (contentType.indexOf("content-type") == -1) { System.out.println(contentType); } else { System.out.println("the head of file" + readLine(bytes, iindex, sis, encoding)); } } boolean tt = false; int mark = 0; byte[] backups = new byte[4096]; while ((tmp = readLine(bytes, iindex, sis, encoding)) != null) { if (endFlag.equals(tmp)) { if (mark > 2) { fos.write(backups, 0, mark - 2); fos.flush(); } break; } else { if (tt) { fos.write(backups, 0, mark); fos.flush(); } mark = iindex[0]; for (int i = 0; i < iindex[0]; i++) { backups[i] = bytes[i]; } tt = true; } } fos.close(); sis.close(); } // 获取上传的文件名 protected String getName(String name) { String rtn = null; if (name != null) { int index = name.lastIndexOf("/"); if (index != -1) { rtn = name.substring(index + 1); } else { index = name.lastIndexOf("\\"); if (index != -1) { rtn = name.substring(index + 1); } else { rtn = name; } } } return rtn; } // 读取每一行 protected String readLine(byte[] bytes, int[] index, ServletInputStream sis, String encoding) { try { index[0] = sis.readLine(bytes, 0, bytes.length); if (index[0] < 0) return null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } try { if (encoding == null) { return new String(bytes, 0, index[0]); } else { return new String(bytes, 0, index[0], encoding); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }
相关文章推荐
- Servlet:通过初始参数实现权限访问某个文件、页面
- Jsp+Servlet实现文件上传下载——前台页面开发
- Servlet:通过初始参数实现权限访问某个文件、页面
- 通过JSP+servlet实现文件上传功能
- Servlet:通过初始参数实现权限访问某个文件、页面
- struts2通过FromData+ajax实现文件上传(页面无刷新)
- servlet 通过 FileItem 实现多文件上传
- Jsp+Servlet文件上传功能实现
- 通过Flash实现ASP.NET多文件上传
- winform中通过FileStream实现将文件上传
- Servlet实现基本文件上传
- 通过Windows批处理脚本实现FTP的多文件自动上传
- 通过Windows批处理脚本实现FTP的多文件自动上传
- Struts2通过commons-fileupload实现文件上传
- Jsp/Servlet:实现文件上传与下载
- CKEditor 3开启文件上传功能(Servlet实现)
- 转载tangl_99的Servlet 实现文件上传下载--自己的一点心得
- JAVA学习提高之---- FileUpload组件实现多文件上传(JSP+SERVLET)实现
- 轻松实现一个页面多文件上传
- 在Servlet中使用开源fileupload包实现文件上传功能 推荐