xheditor-文件上传-java-支持html5-application/octet-stream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | package reyo.sdk.utils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Serializable; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.DiskFileUpload; import org.apache.commons.fileupload.FileItem; import org.apache.commons.lang.StringUtils; public class UploadFileServlet extends HttpServlet { private static final long serialVersionUID = 1541334866883495283L; private static String baseDir = "/UploadFile/" ; // 上传文件存储目录 private static String fileExt = "jpg,jpeg,bmp,gif,png,txt" ; private static Long maxSize = 0l; private static String dirType = "1" ; // 0:不建目录 1:按天存入目录 2:按月存入目录 3:按扩展名存目录 // 建议使用按天存 public void init() throws ServletException { baseDir = this .getInitParameter( "baseDir" ); // 获取web.xml中servlet的配置文件目录参数 if (StringUtils.isEmpty(baseDir)) baseDir = "/UploadFile/" ; // 获取文件上传存储的相当路径 String realBaseDir = this .getServletConfig().getServletContext().getRealPath(baseDir); File baseFile = new File(realBaseDir); if (!baseFile.exists()) { baseFile.mkdir(); } fileExt = this .getInitParameter( "fileExt" ); // 获取文件类型参数 if (StringUtils.isEmpty(fileExt)) fileExt = "jpg,jpeg,gif,bmp,png,txt" ; String maxSize_str = this .getInitParameter( "maxSize" ); // 获取文件大小参数 if (StringUtils.isNotEmpty(maxSize_str)) { maxSize = new Long(maxSize_str); } else { maxSize = Long.valueOf( "5242880" ); // 5M } dirType = this .getInitParameter( "dirType" ); // 获取文件目录类型参数 if (StringUtils.isEmpty(dirType)) dirType = "1" ; if ( ",0,1,2,3," .indexOf( "," + dirType + "," ) < 0 ) dirType = "1" ; } // 上传文件数据处理过程 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "text/html; charset=UTF-8" ); response.setHeader( "Cache-Control" , "no-cache" ); String err = "" ; String newFileName = "" ; if ( "application/octet-stream" .equals(request.getContentType())) { // HTML // 5 // 上传 try { String dispoString = request.getHeader( "Content-Disposition" ); int iFindStart = dispoString.indexOf( "name=\"" ) + 6 ; int iFindEnd = dispoString.indexOf( "\"" , iFindStart); iFindStart = dispoString.indexOf( "filename=\"" ) + 10 ; iFindEnd = dispoString.indexOf( "\"" , iFindStart); String sFileName = dispoString.substring(iFindStart, iFindEnd); int i = request.getContentLength(); byte buffer[] = new byte [i]; int j = 0 ; while (j < i) { // 获取表单的上传文件 int k = request.getInputStream().read(buffer, j, i - j); j += k; } if (buffer.length == 0 ) { // 文件是否为空 printInfo(response, "上传文件不能为空" , "" ); return ; } if (maxSize > 0 && buffer.length > maxSize) { // 检查文件大小 printInfo(response, "上传文件的大小超出限制" , "" ); return ; } String filepathString = getSaveFilePath(sFileName, response); if ( "不允许上传此类型的文件" .equals(filepathString)) return ; // 检查文件类型 OutputStream out = new BufferedOutputStream( new FileOutputStream( this .getServletConfig().getServletContext().getRealPath( "" ) + filepathString, true )); out.write(buffer); out.close(); newFileName = request.getContextPath() + filepathString; } catch (Exception ex) { System.out.println(ex.getMessage()); newFileName = "" ; err = "错误: " + ex.getMessage(); } } else { DiskFileUpload upload = new DiskFileUpload(); try { List<FileItem> items = upload.parseRequest(request); Map<String, Serializable> fields = new HashMap<String, Serializable>(); Iterator<FileItem> iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (item.isFormField()) fields.put(item.getFieldName(), item.getString()); else fields.put(item.getFieldName(), item); } FileItem uploadFile = (FileItem) fields.get( "filedata" ); // 获取表单的上传文件 String fileNameLong = uploadFile.getName(); // 获取文件上传路径名称 if (uploadFile.getSize() == 0 ) { // 文件是否为空 printInfo(response, "上传文件不能为空" , "" ); return ; } if (maxSize > 0 && uploadFile.getSize() > maxSize) { // 检查文件大小 printInfo(response, "上传文件的大小超出限制" , "" ); return ; } String filepathString = getSaveFilePath(fileNameLong, response); if ( "不允许上传此类型的文件" .equals(filepathString)) return ; // 检查文件类型 File savefile = new File( this .getServletConfig().getServletContext().getRealPath( "" ) + filepathString); uploadFile.write(savefile); // 存储上传文件 newFileName = request.getContextPath() + filepathString; } catch (Exception ex) { System.out.println(ex.getMessage()); newFileName = "" ; err = "错误: " + ex.getMessage(); } } printInfo(response, err, newFileName); } public String getSaveFilePath(String sFileName, HttpServletResponse response) throws IOException { String extensionName = sFileName.substring(sFileName.lastIndexOf( "." ) + 1 ); // 获取文件扩展名 if (( "," + fileExt.toLowerCase() + "," ).indexOf( "," + extensionName.toLowerCase() + "," ) < 0 ) { // 检查文件类型 printInfo(response, "不允许上传此类型的文件" , "" ); return "不允许上传此类型的文件" ; } String fileFolder = "" ; // 0:不建目录, 1:按天存入目录, 2:按月存入目录, // 3:按扩展名存目录.建议使用按天存。 if (dirType.equalsIgnoreCase( "1" )) fileFolder = new SimpleDateFormat( "yyyyMMdd" ).format( new Date()); if (dirType.equalsIgnoreCase( "2" )) fileFolder = new SimpleDateFormat( "yyyyMM" ).format( new Date()); if (dirType.equalsIgnoreCase( "3" )) fileFolder = extensionName.toLowerCase(); String saveDirPath = baseDir + fileFolder + "/" ; // 文件存储的相对路径 String saveFilePath = this .getServletConfig().getServletContext().getRealPath( "" ) + saveDirPath; // 文件存储在容器中的绝对路径 File fileDir = new File(saveFilePath); // 构建文件目录以及目录文件 if (!fileDir.exists()) { fileDir.mkdirs(); } String filename = UUID.randomUUID().toString(); // 重命名文件 return saveDirPath + filename + "." + extensionName; } // 使用I/O流输出 json格式的数据 public void printInfo(HttpServletResponse response, String err, String newFileName) throws IOException { response.setContentType( "text/plain" ); response.setCharacterEncoding( "UTF-8" ); PrintWriter out = response.getWriter(); out.println( "{\"err\":\"" + err + "\",\"msg\":\"" + newFileName + "\"}" ); out.flush(); out.close(); } } |
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
· Windows 提权-UAC 绕过
2014-06-13 Java 复制大文件方式(nio2 FileChannel 拷贝文件能力测试)