您好,欢迎光临本网站![请登录][注册会员]  
文件名称: java文件上传和下载的包及样例
  所属分类: Java
  开发工具:
  文件大小: 121kb
  下载次数: 0
  上传时间: 2009-07-10
  提 供 者: Fair****
 详细说明: mport java.io.*; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; public class AttachUploadServlet { public static final int MAX_SIZE = 1024 * 1024 * 100; public static final String FILE_DIR = "d:/temp/"; private int file_ Size = 0; private String file_Path = ""; private HashMap hm = new HashMap(); public String upLoad(HttpServletRequest req) { String tmpString = ""; String result = ""; DataInputStream dis = null; try { dis = new DataInputStream(req.getInputStream()); String content = req.getContentType(); if (content != null && content.indexOf("multipart/form-data") != -1) { int reqSize = req.getContentLength(); byte[] data = new byte[reqSize]; int bytesRead = 0; int totalBytesRead = 0; int sizeCheck = 0; while (totalBytesRead < reqSize) { // check for maximum file size violation sizeCheck = totalBytesRead + dis.available(); if (sizeCheck > MAX_SIZE) result = "文件太大不能上传..."; bytesRead = dis.read(data, totalBytesRead, reqSize); totalBytesRead += bytesRead; } tmpString = new String(data); hm = parseAnotherParam(tmpString); int postion = arrayIndexOf(data, "\r\n".getBytes()); byte[] split_arr = new byte[postion]; System.arraycopy(data, 0, split_arr, 0, postion); postion = arrayIndexOf(data, "filename=\"".getBytes()); byte[] dataTmp = new byte[data.length - postion]; System.arraycopy(data, postion, dataTmp, 0, dataTmp.length); data = null; data = dataTmp.clone(); String filePath = null; postion = arrayIndexOf(data, "Content-Type:".getBytes()) - 2; dataTmp = null; dataTmp = new byte[postion]; System.arraycopy(data, 0, dataTmp, 0, dataTmp.length); filePath = new String(dataTmp); if (filePath == null && filePath.equals("")) return ""; // 分离contentType 并赋值 postion = arrayIndexOf(data, "Content-Type:".getBytes()); dataTmp = null; dataTmp = new byte[data.length - postion]; System.arraycopy(data, postion, dataTmp, 0, dataTmp.length); data = null; data = dataTmp.clone(); postion = arrayIndexOf(data, "\n".getBytes()) + 1; dataTmp = null; dataTmp = new byte[data.length - postion]; System.arraycopy(data, postion, dataTmp, 0, dataTmp.length); data = null; data = dataTmp.clone(); // 分离文件信息 获得最终想要的字节 postion = arrayIndexOf(data, split_arr); split_arr = null; dataTmp = null; dataTmp = new byte[postion - 2]; System.arraycopy(data, 2, dataTmp, 0, dataTmp.length); data = null; data = dataTmp.clone(); postion = arrayLastIndexOf(data, "\n".getBytes()) - 1; dataTmp = null; dataTmp = new byte[postion]; System.arraycopy(data, 0, dataTmp, 0, dataTmp.length); data = null; String file_path = getFileName(filePath); if (null != file_path) { if (writeFile(dataTmp, FILE_DIR + file_path)) { this.file_Size = dataTmp.length; this.file_Path = FILE_DIR + file_path; result = "文件上传完毕"; } else { result = "文件上传失败"; } } else { result = "文件名为空"; } dataTmp = null; } else { result = "content 必须为 multipart/form-data"; } } catch (UnsupportedEncodingException ex4) { result = "UnsupportedEncodingException错误"; } catch (NullPointerException e) { result = "NullPointerException错误"; } catch (IOException ex1) { result = "IOException 错误 "; } catch (Exception ex1) { result = "Exception 错误 "; } return result; } public String getFilePath() { return this.file_Path; } public int getFileSize() { return this.file_Size; } public boolean writeFile(byte[] data, String path) { File f = null; FileOutputStream fos = null; try { f = new File(path); f.createNewFile(); fos = new FileOutputStream(f); fos.write(data, 0, data.length); } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } finally { try { fos.close(); } catch (IOException e) { return false; } } return true; } public String getFileName(String arg) { String path = ""; if (arg.equals("\"\"")) { return null; } if (arg.indexOf("\"") > -1) path = arg.substring(arg.indexOf("\"") + 1, arg.lastIndexOf("\"")); else path = arg; path = path.substring(path.lastIndexOf("\\") + 1); return path; } //判断两个byte数组的值是否相等 private boolean arrayEquals(byte[] src, byte[] value) { if (src == null || value == null) return false; if (src.length != value.length) return false; for (int i = 0; i < src.length; i++) { if (src[i] != value[i]) return false; } return true; } //找出value数组在src中的位置, 从前往后 private int arrayIndexOf(byte[] src, byte[] value) { if (src == null || value == null) return -1; if (src.length < value.length) return -1; int postion = -1; for (int i = 0; i < src.length - value.length; i++) { postion = i; byte[] tmp = new byte[value.length]; System.arraycopy(src, i, tmp, 0, tmp.length); if (arrayEquals(tmp, value)) { tmp = null; return postion; } else { postion = -1; tmp = null; } } return postion; } //找出value数组在src中的位置 private int arrayLastIndexOf(byte[] src, byte[] value) { if (src == null || value == null) return -1; if (src.length < value.length) return -1; int postion = -1; for (int i = src.length - value.length; i > -1; i--) { postion = i; byte[] tmp = new byte[value.length]; System.arraycopy(src, i, tmp, 0, tmp.length); if (arrayEquals(tmp, value)) { tmp = null; return postion; } else { postion = -1; tmp = null; } } return postion; } public HashMap parseAnotherParam(String str) { HashMap hm = new HashMap(); String key = ""; String value = ""; int startindex = 0; int endindex = 0; startindex = str.indexOf("Content-Disposition: form-data; name=\"") + "Content-Disposition: form-data; name=\"".length(); endindex = str.indexOf("\"\r\n\r\n"); while (startindex > -1 && endindex > -1) { key = str.substring(startindex, endindex); if (!str.substring(endindex, endindex + 5).equals("\"\r\n\r\n")) {//去掉没有value的元素 str = str.substring(endindex); startindex = str .indexOf("Content-Disposition: form-data; name=\"") + "Content-Disposition: form-data; name=\"".length(); endindex = str.indexOf("\"\r\n\r\n"); continue; } if (key.indexOf("\";") > -1) {//去掉上传文件的参数以及编码 str = str.substring(str.indexOf("\";") + 2); startindex = str .indexOf("Content-Disposition: form-data; name=\"") + "Content-Disposition: form-data; name=\"".length(); endindex = str.indexOf("\"\r\n\r\n"); continue; } else str = str.substring(endindex + 5); value = str.substring(0, str.indexOf("\r\n")); str = str.substring(str.indexOf("\r\n") + 2); hm.put(key, value); startindex = str.indexOf("Content-Disposition: form-data; name=\"") + "Content-Disposition: form-data; name=\"".length(); endindex = str.indexOf("\"\r\n\r\n"); } return hm; } public String getParameter(String param) { return (String) hm.get(param); } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: 文件上传和下载的包
 输入关键字,在本站1000多万海量源码库中尽情搜索: