fileupload.java
来自「cwbbs 云网论坛源码」· Java 代码 · 共 622 行 · 第 1/2 页
JAVA
622 行
realPath += "/"; ret = RET_SUCCESS; files.removeAllElements(); int allFileSize = 0; ServletInputStream in = request.getInputStream(); final int maxcount = 2048; byte[] line = new byte[maxcount]; byte[] oldline = new byte[maxcount]; int oldi; int i = in.readLine(line, 0, maxcount); if (debug) { File f2 = new File("c:/redmoon_upload.txt"); FileOutputStream os2; os2 = new FileOutputStream(f2); OutputStreamWriter osw = new OutputStreamWriter(os2, charset); while (i != -1) { String d = new String(line, 0, i, charset); osw.write(d); i = in.readLine(line, 0, maxcount); System.out.println(d); } osw.close(); os2.close(); if (true) { return -1; } } if (i < 3) { ret = this.RET_FAIL; return ret; } int boundaryLength = i - 2; String boundary = new String(line, 0, boundaryLength); fields = new Hashtable(); if (tmpPath==null) { tmpPath = realPath + "FileUploadTmp/"; File f = new File(tmpPath); if (!f.isDirectory()) { f.mkdirs(); } } while (i != -1) { String newLine = new String(line, 0, i); if (newLine.startsWith("Content-Disposition:")) { if (newLine.indexOf("filename=\"") != -1) { int pos = newLine.indexOf("name=\""); String fieldName = newLine.substring(pos + 6, newLine.length() - 3); int index = fieldName.indexOf("\";"); fieldName = fieldName.substring(0, index); fieldName = new String(fieldName.getBytes(), charset); parseFileName(new String(line, 0, i - 2, charset)); if (filename == null) { i = in.readLine(line, 0, maxcount); continue; } if (filename != null && !isValidExtname(extname)) { if (debug) { System.out.println("extname=" + extname); } ret = this.RET_INVALIDEXT; return ret; } i = in.readLine(line, 0, maxcount); setContentType(new String(line, 0, i - 2)); i = in.readLine(line, 0, maxcount); i = in.readLine(line, 0, maxcount); newLine = new String(line, 0, i); long thisfilesize = 0; String tmpFileName = getRandName() + "." + extname; String tmpFilePath = tmpPath + tmpFileName; tmpFiles.addElement(tmpFilePath); File tmpFile = new File( tmpFilePath ); FileOutputStream os2; os2 = new FileOutputStream(tmpFile); while (i != -1 && !newLine.startsWith(boundary)) { oldi = i; for (int k = 0; k < i; k++) { oldline[k] = line[k]; } i = in.readLine(line, 0, maxcount); if ((i == boundaryLength + 2 || i == boundaryLength + 4) && (new String(line, 0, i).startsWith(boundary))) { os2.write(oldline, 0, oldi-2); allFileSize += oldi - 2; thisfilesize += oldi - 2; } else { os2.write(oldline, 0, oldi); allFileSize += oldi; thisfilesize += oldi; } newLine = new String(line, 0, i); if (thisfilesize > fileSize * 1024) { in.close(); tmpFile.delete(); os2.close(); ret = this.RET_TOOLARGESINGLE; return ret; } if (maxAllFileSize != -1 && allFileSize > maxAllFileSize * 1024) { ret = this.RET_TOOLARGEALL; tmpFile.delete(); os2.close(); return ret; } } os2.close(); if (thisfilesize == 0) { if (debug) { System.out.println("FileUpload 文件" + filename + "长度为 0 !"); } continue; } FileInfo fi = new FileInfo(); fi.fieldName = fieldName; fi.name = filename; fi.ext = extname; fi.setTmpFilePath(tmpFilePath); fi.contentType = contentType; fi.size = thisfilesize; if (debug) { System.out.println(fi.name + ": " + fi.size + " " + fi.ext + " " + fi.contentType); } files.addElement(fi); } else { int pos = newLine.indexOf("name=\""); String fieldName = newLine.substring(pos + 6, newLine.length() - 3); i = in.readLine(line, 0, maxcount); String seperateLine = new String(line, 0, i, charset); if (seperateLine.startsWith("Content-Type")) i = in.readLine(line, 0, maxcount); i = in.readLine(line, 0, maxcount); newLine = new String(line, 0, i, charset); StringBuffer fieldValue = new StringBuffer(maxcount); while (i != -1 && !newLine.startsWith(boundary)) { i = in.readLine(line, 0, maxcount); if ((i == boundaryLength + 2 || i == boundaryLength + 4) && (new String(line, 0, i).startsWith(boundary))) { fieldValue.append(newLine.substring(0, newLine.length() - 2)); } else { fieldValue.append(newLine); } newLine = new String(line, 0, i, charset); } Object obj = fields.get(fieldName); if (obj!=null) { if (obj instanceof String) { Vector v = new Vector(); v.addElement(obj); v.addElement(fieldValue.toString()); fields.put(fieldName, v); } else { Vector v = (Vector) obj; v.addElement(fieldValue.toString()); } } else fields.put(fieldName, fieldValue.toString()); } } i = in.readLine(line, 0, maxcount); } in.close(); return ret; } public void writeFile(boolean isRandName) { int size = files.size(); if (size == 0) { return; } File f = new File(savePath); if (!f.isDirectory()) { f.mkdirs(); } java.util.Enumeration e = files.elements(); while (e.hasMoreElements()) { FileInfo fi = (FileInfo) e.nextElement(); if (!isRandName) { fi.write(savePath); } else { fi.write(savePath, getRandName() + "." + fi.getExt()); } } } public static String getRandName() { if (System.currentTimeMillis()-lastRandTime>20000) hash.clear(); Integer id = new Integer(0); synchronized (hash) { id = new Integer(rand.nextInt()); while (hash.containsKey(id)) { id = new Integer(rand.nextInt()); } String data = ""; hash.put(id, data); } lastRandTime = System.currentTimeMillis(); return System.currentTimeMillis() + "" + Math.abs(id.intValue()); } public static String getUploadFileName(String filePath) { String fileName = ""; int pos = filePath.lastIndexOf("\\"); if (pos != -1) { fileName = filePath.substring(pos + 1); } else { fileName = filePath; } return fileName; } public static String getFileExt(String fileName) { int dotindex = fileName.lastIndexOf("."); String extName = fileName.substring(dotindex + 1, fileName.length()); extName = extName.toLowerCase(); return extName; } private int maxAllFileSize = -1; private String realPath; private static String tmpPath = null; private boolean jspValid = false; private boolean htmValid = false; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?