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.setProgressListener(getBarListener);
		try
		{
			List<FileItem> formList = upload.parseRequest(req);
			Iterator<FileItem> 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();
		}
	}

 

 

 

 

 

 

 

 

 

 

 

 

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