「java多文件下载进度条」Java进度条
今天给各位分享java多文件下载进度条的知识,其中也会对Java进度条进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java编程:怎么写进度条?高手进哈
进度条有三种思路:
1.人为的划分某些标识,达到某个标识就是完成了百分之多少。
2.如果是上传和下载附件,可以将文件大小作为100%,上传或下载百分之多少,就是百分之多少。
3.对进度的类型所需时间进行分类。划出几种时间。属于某类,大概或平均完成时间是多少,那么就以这个时间作为100%。然后产生进度。
但,完美的进度是不可能实现的,因为你总不能先跑一遍确定时间。而且就算完全相同的条件,跑两遍的时间也不能一定相等的。所以我们只能通过各种技巧来使进度条更加自然真实。
最后,现在很多地方都不用进度条了,全部都是一个转动的圆圈等等。因为当你进度卡在10%半个小时,然后瞬间涨到99%,进度条已经就没什么意义了。
请问一个java的文件下载的问题,为什么我的下载每次都看不到进度条,而是一下子就下好了呢?
建立新的线程来处理下载。去看Thread的API有详细说明。例如本来是:
downloading();
System.out.println("下载完成");
就改成:
class DownloadListener {//这个class自己找个地方放。
public void downloadInProgress(double percent) {
System.out.println("已下载" + percent);
}
public void downloadCompleted() {
System.out.println("已下载完成");
}
}
//从这里开始应该被改成的内容
DownloadListener downloadListener = new DownloadListener();
new Thread() {
public void run() { //这里写第二线程的内容
downloading(downloadListener);//于是downloadListener被传入了downloading函数,就可以在downloading函数内部通过呼叫downloadListener的downloadInProgress和downloadCompleted来通知第一线程下载情况。
}
}.start(); //第一线程会在这里启动第二线程,然后不管第二线程执行了什么是否完成,直接执行下一句。
System.out.println("第二线程已经启动");
//例子里很多System.out.println是为了简明。实际情况一般是修改一个给用户看的状态栏JLabel,这里下载进行中时,应该是更新一个JProgressBar。
java多文件上传显示进度条
使用 apache fileupload ,spring MVC jquery1.6x , bootstrap 实现一个带进度条的多文件上传,由于fileupload 的局限,暂不能实现每个上传文件都显示进度条,只能实现一个总的进度条,效果如图:
1、jsp 页面
!DOCTYPE html
%@ page contentType="text/html;charset=UTF-8"%
%@ taglib prefix="c" uri="" %
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /
script src="../js/jquery-1.6.4.js" type="text/javascript"/script
link rel="stylesheet" type="text/css" href="../css/bootstrap.css"/
/head
body
form id='fForm' class="form-actions form-horizontal" action="../upload.html"
encType="multipart/form-data" target="uploadf" method="post"
div class="control-group"
label class="control-label"上传文件:/label
div class="controls"
input type="file" name="file" style="width:550"
/div
div class="controls"
input type="file" name="file" style="width:550"
/div
div class="controls"
input type="file" name="file" style="width:550"
/div
label class="control-label"上传进度:/label
div class="controls"
div class="progress progress-success progress-striped" style="width:50%"
div id = 'proBar' class="bar" style="width: 0%"/div
/div
/div
/div
div class="control-group"
div class="controls"
button type="button" id="subbut" class="btn"submit/button
/div
/div
/form
iframe name="uploadf" style="display:none"/iframe
/body
/html
script
$(document).ready(function(){
$('#subbut').bind('click',
function(){
$('#fForm').submit();
var eventFun = function(){
$.ajax({
type: 'GET',
url: '../process.json',
data: {},
dataType: 'json',
success : function(data){
$('#proBar').css('width',data.rate+''+'%');
$('#proBar').empty();
$('#proBar').append(data.show);
if(data.rate == 100){
window.clearInterval(intId);
}
}});};
var intId = window.setInterval(eventFun,500);
});
});
/script
2、java 代码
package com.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class FileUploadController {
Logger log = Logger.getLogger(FileUploadController.class);
/**
* upload 上传文件
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = "/upload.html", method = RequestMethod.POST)
public ModelAndView upload(HttpServletRequest request,
HttpServletResponse response) throws Exception {
final HttpSession hs = request.getSession();
ModelAndView mv = new ModelAndView();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(!isMultipart){
return mv;
}
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setProgressListener(new ProgressListener(){
public void update(long pBytesRead, long pContentLength, int pItems) {
ProcessInfo pri = new ProcessInfo();
pri.itemNum = pItems;
pri.readSize = pBytesRead;
pri.totalSize = pContentLength;
pri.show = pBytesRead+"/"+pContentLength+" byte";
pri.rate = Math.round(new Float(pBytesRead) / new Float(pContentLength)*100);
hs.setAttribute("proInfo", pri);
}
});
List items = upload.parseRequest(request);
// Parse the request
// Process the uploaded items
// Iterator iter = items.iterator();
// while (iter.hasNext()) {
// FileItem item = (FileItem) iter.next();
// if (item.isFormField()) {
// String name = item.getFieldName();
// String value = item.getString();
// System.out.println("this is common feild!"+name+"="+value);
// } else {
// System.out.println("this is file feild!");
// String fieldName = item.getFieldName();
// String fileName = item.getName();
// String contentType = item.getContentType();
// boolean isInMemory = item.isInMemory();
// long sizeInBytes = item.getSize();
// File uploadedFile = new File("c://"+fileName);
// item.write(uploadedFile);
// }
// }
return mv;
}
/**
* process 获取进度
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = "/process.json", method = RequestMethod.GET)
@ResponseBody
public Object process(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return ( ProcessInfo)request.getSession().getAttribute("proInfo");
}
class ProcessInfo{
public long totalSize = 1;
public long readSize = 0;
public String show = "";
public int itemNum = 0;
public int rate = 0;
}
}
java多文件下载进度条的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java进度条、java多文件下载进度条的信息别忘了在本站进行查找喔。
发布于:2022-11-25,除非注明,否则均为
原创文章,转载请注明出处。