java file 进度条 文件上传

阅读更多

java  file 进度条

 

 

public class MyProgressListener implements ProgressListener
	{
		private HttpSession session;

		public MyProgressListener(HttpServletRequest req)
		{
			session = req.getSession();
			FileUploadStatus status = new FileUploadStatus();
			session.setAttribute("status", status);
		}

		/*
		 * pBytesRead 到目前为止读取文件的比特数 pContentLength 文件总大小 pItems 目前正在读取第几个文件
		 */
		public void update(long pBytesRead, long pContentLength, int pItems)
		{
			FileUploadStatus status = (FileUploadStatus) session.getAttribute("status");
			status.setPBytesRead(pBytesRead);
			status.setPContentLength(pContentLength);
			status.setPItems(pItems);
		}
	}

 

 

 

public void uploadFile(HttpServletRequest req, HttpServletResponse rsp)
	{
		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(2048 * 1024);
		MyProgressListener getBarListener = new MyProgressListener(req);
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setFileSizeMax(2 * 1024 * 1024);
                upload.setHeaderEncoding("UTF-8"); 
		upload.setProgressListener(getBarListener);
		try
		{
			List formList = upload.parseRequest(req);
			Iterator formItem = formList.iterator();
			// 将进度监听器加载进去
			while (formItem.hasNext())
			{
				FileItem item = (FileItem) formItem.next();
				String originalNamePath = item.getName();
				if (originalNamePath == null || originalNamePath.equals("")){continue;}
				int index = originalNamePath.lastIndexOf("\\");
				String originalName = "";
				if (index != -1){
					originalName = originalNamePath.substring(index + 1);
				}else{
					originalName = originalNamePath;
				}
				this.getSession().setAttribute(ORIGINAL_NAME, originalName);
				if (item.isFormField()){
					logger.info("Field Name:" + item.getFieldName());
				}
				else{
					String filePostfix = item.getName().substring(item.getName().lastIndexOf(".") + 1).toLowerCase();
					if (!(ArraysUtil.isIncludeStrings(new String[] { "bmp", "png", "jpeg", "jpg", "gif" }, filePostfix)))
					{
						// 文件格式不对
						try
						{
							String rt = "{\"statuscode\":401}";
							rsp.getWriter().write(rt);
							break;
						}
						catch (Exception e)
						{
						}
					}
					String icoName = Long.toString(DatetimeUtil.getUniqueTimestamp());
					String filenamew = "mulit" + icoName + "." + filePostfix;
					File file = new File(FileUtil.getTomcatRootPath(req) + File.separatorChar + filenamew);
					logger.info(FileUtil.getTomcatRootPath(req) + File.separatorChar + filenamew);
					item.write(file);
					this.getSession().setAttribute("oafile", filenamew);
					logger.info("save oafile " + filenamew);
					try
					{
						String rt1 = "{\"statuscode\":200,\"filename\":\"" + filenamew + "\"}";
						rsp.getWriter().write(rt1);
						break;
					}
					catch (Exception e)
					{
					}
				}
			}
		}
		catch (FileSizeLimitExceededException e1)
		{
			logger.warn("", e1);
			e1.printStackTrace();
			try
			{
				String rt = "{\"statuscode\":400}";
				rsp.getWriter().write(rt);
			}
			catch (IOException e)
			{
				logger.warn("", e);
			}
		}
		catch (FileUploadException e2)
		{
			logger.warn("", e2);
			e2.printStackTrace();
		}
		catch (Exception e)
		{
			logger.warn("", e);
			e.printStackTrace();
		}
	}

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。


java file 进度条 文件上传_第1张图片java file 进度条 文件上传_第2张图片java file 进度条 文件上传_第3张图片
 
 
 谢谢您的赞助,我会做的更好!

 

 

你可能感兴趣的:(file,进度条,上传,文件,upload)