日期:2014-05-17  浏览次数:20850 次

java进度条问题?
想在传文件时做一个进度条显示传输的比例,现在有个问题,本地显示的进度条,刚开始时,怎么获得远程传过来的文件的大小?
请高手们指点

------解决方案--------------------
lz可以感觉上传文件读取的流来判断上传多少,和文件总共有多少字节

然后用ajax每0.5秒请求一次并判断上传多少百分比

进度条用css和百分比来判断长度

提交上传方法,ajax访问读取流数据的方法。

这样就可以了

原理很简单


------解决方案--------------------
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 * 看看我的是用commons fileupload组件
 */
package com.oy.struts.action;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.oy.image.model.LoginUser;
import com.oy.image.service.ImageManager;


public class KKAction extends Action {
private static final long serialVersionUID = 2431664260164700200L;
private ServletContext sc;
private String savePath;
private ImageManager imageManager;
 

public void setImageManager(ImageManager imageManager) {
this.imageManager = imageManager;
}

/**
* Method execute

* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {

savePath =request.getSession().getServletContext().getInitParameter("uploads");

//savePath=config.getServletContext().getInitParameter("savePath");
System.out.println("+++++++++++++++++++"+savePath);
// if(!new File(savePath).isDirectory())
System.out.println(new File(savePath).getAbsolutePath());
if(!new File(savePath).exists())
{

new File(savePath).mkdirs();
}
sc = request.getSession().getServletContext();



DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
String userName = null;
String url = null;
try {
List items = upload.parseRequest(request);
Iterator it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (item.isFormField()) {
userName = item.getString("UTF-8");
System.out.println("表单的参数名称:" + item.getFieldName() + ",对应的参数值:"
+ item.getString("UTF-8"));
} else {
if (item.getName() != null && !item.getName().equals("")) {

System.out.println("上传文件的大小:" + item.getSize());
System.out.println("上传文件的类型:" + item.getContentType());
System.out.println("上传文件的名称:" + item.getName());

File tempFile = new File(item.getName());
File file = new File(sc.getRealPath("/") + savePath, tempFi