这个事多线程下载一个文件
成都创新互联公司专业提供绵阳服务器托管服务,为用户提供五星数据中心、电信、双线接入解决方案,用户可自行在线购买绵阳服务器托管服务,并享受7*24小时金牌售后服务。
这个是同时下载多个文件得
希望对你有用,多线程下载这玩意没写过,不过,可以给你个思路,不知正确与否,希望与你有用:
对同一个文件或http建立connection,然后获取整个文件得大小,(只有知道文件大小才能同时使用多线程建立多个连接来分段下载,如果不知道文件大小,想用多线程下载一定要做好线程同步工作,且速度提高不快,而且只能用线程轮流读取同一个输入流,而不能用不同得线程做不同得连接来下载),然后执行数据分段计算,算出每个线程建立连接后需要从哪个偏移量进行下载(需要使用文件得大小除以给定的线程数来算出),之后进行连接,每个线程建立连接后根据算出得下载偏移量将数据指针移到需要下载得位置开始下载,当所有线程下载完毕后再将每个线程下载得文件按下载得前后顺序(这里得顺序意思用一个例子来说明:比如a线程负责下载0到99字节,b线程负责下载100到199字节,c线程下载200到299字节,那么当所有线程下载完毕后需要将先将b线程下载好得文件增加到a线程下载得文件中,再将c线程下载得文件增加到a线程下载得文件中,然后删除b,c线程下载得文件.)合成一个文件.
上面只是我个人意见,不知道是否正确,呵呵.
愿上帝保佑吧.呵呵.
最简单的java代码肯定就是这个了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是应该是所有学java的新手看的第一个代码了。如果是零基础的新手朋友们可以来我们的java实验班试听,有免费的试听课程帮助学习java必备基础知识,有助教老师为零基础的人提供个人学习方案,学习完成后有考评团进行专业测试,帮助测评学员是否适合继续学习java,15天内免费帮助来报名体验实验班的新手快速入门java,更好的学习java!
您好,这样的:
/**
*
*/
package com.b510.base.bean.install;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author hongten(hongtenzone@foxmail.com)
* @date 2013-2-24
*/
@SuppressWarnings("unchecked")
public class BeanUtils {
//公共部分
private static final String RT_1 = "\r\n";
private static final String RT_2 = RT_1+RT_1;
private static final String BLANK_1 =" ";
private static final String BLANK_4 =" ";
private static final String BLANK_8 =BLANK_4 + BLANK_4;
//注释部分
private static final String ANNOTATION_AUTHOR_PARAMTER = "@author ";
private static final String ANNOTATION_AUTHOR_NAME = "hongten(hongtenzone@foxmail.com)";
private static final String ANNOTATION_AUTHOR = ANNOTATION_AUTHOR_PARAMTER + ANNOTATION_AUTHOR_NAME;
private static final String ANNOTATION_DATE = "@date ";
private static final String ANNOTATION = "/**"+RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_AUTHOR +RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_DATE +getDate()+RT_1+BLANK_1+"*/"+RT_1;
//文件 地址
//private static final String BEAN_PATH = "com/b510/base/bean";
private static final String DAO_PATH = "com/b510/base/dao";
private static final String DAO_IMPL_PATH = "com/b510/base/dao/impl";
private static final String SERVICE_PATH = "com/b510/base/service";
private static final String SERVICE_IMPL_PATH = "com/b510/base/service/impl";
//包名
private static final String BEAN_URL = "com.b510.base.bean";
private static final String DAO_URL = "com.b510.base.dao";
private static final String DAO_IMPL_URL = "com.b510.base.dao.impl";
private static final String SERVICE_URL = "com.b510.base.service";
private static final String SERVICE_IMPL_URL = "com.b510.base.service.impl";
//基本类名称
private static final String BASE_DAO_NAME = DAO_URL + ".BaseDao";
private static final String ABSTRACT_BASE_DAO_IMPL_NAME = DAO_IMPL_URL + ".AbstractBaseDaoImpl";
private static final String BASE_SERVICE_NAME = SERVICE_URL + ".BaseService";
private static final String ABSTRACT_BASE_SERVICE_IMPL_NAME = SERVICE_IMPL_URL + ".AbstractBaseServiceImpl";
/**
* 创建bean的Daobr
*
* @param c
* @throws Exception
*/
public void createBeanDao(Class c) throws Exception {
String cName = c.getName();
String fileName = System.getProperty("user.dir") + "/src/" + DAO_PATH
+ "/" + getLastChar(cName) + "Dao.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write("package "+DAO_URL+";"+RT_2+ANNOTATION+"public interface " +
getLastChar(cName) + "Dao extends "+BASE_DAO_NAME+" " + cName + " {"+RT_2+"}");
fw.flush();
fw.close();
showInfo(fileName);
}
/**
* 创建bean的Dao的实现类
* @param c
* @throws Exception
*/
public void createBeanDaoImpl(Class c) throws Exception{
String cName = c.getName();
String fileName = System.getProperty("user.dir") + "/src/" + DAO_IMPL_PATH
+ "/" + getLastChar(cName) + "DaoImpl.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write("package "+DAO_IMPL_URL+";"+RT_2+ANNOTATION+"public class " +
getLastChar(cName) + "DaoImpl extends "+ABSTRACT_BASE_DAO_IMPL_NAME+"" +
cName + " implements "+DAO_URL+"."+getLastChar(cName)+"Dao{"+RT_2+"}");
fw.flush();
fw.close();
showInfo(fileName);
}
/**
* 创建bean的service
* @param c
* @throws Exception
*/
public void createBeanService(Class c) throws Exception{
String cName = c.getName();
String fileName = System.getProperty("user.dir") + "/src/" + SERVICE_PATH
+ "/" + getLastChar(cName) + "Service.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write("package "+SERVICE_URL+";"+RT_2+ANNOTATION+"public interface " +
getLastChar(cName) + "Service extends "+BASE_SERVICE_NAME+""+ cName +"{"+RT_2+"}");
fw.flush();
fw.close();
showInfo(fileName);
}
/**
* 创建bean的service的实现类
* @param c
* @throws Exception
*/
public void createBeanServiceImpl(Class c) throws Exception{
String cName = c.getName();
String fileName = System.getProperty("user.dir") + "/src/" + SERVICE_IMPL_PATH
+ "/" +getLastChar(cName)+"ServiceImpl.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write("package "+SERVICE_IMPL_URL+";"+RT_2+ANNOTATION+"public class "
+ getLastChar(cName) + "ServiceImpl extends "+ABSTRACT_BASE_SERVICE_IMPL_NAME+""+ cName
+ " implements "+SERVICE_URL+"."+getLastChar(cName)+"Service{"+RT_2+BLANK_4
+"private "+DAO_URL+"."+getLastChar(cName)+"Dao "+getLowercaseChar(getLastChar(cName))
+"Dao;"+RT_2+BLANK_4+"public void set"+getLastChar(cName)+"Dao("+DAO_URL+"."+getLastChar(cName)+"Dao "
+getLowercaseChar(getLastChar(cName))+"Dao){"+RT_1+BLANK_8+"this."+getLowercaseChar(getLastChar(cName))+"Dao = "
+getLowercaseChar(getLastChar(cName))+"Dao;"+RT_1+BLANK_4+"}"+RT_2+BLANK_4+"@Override"+RT_1+BLANK_4
+"public "+DAO_URL+"."+"BaseDao"+BEAN_URL+"."+getLastChar(cName)+" getBaseDao(){"+RT_1+BLANK_8
+"return "+getLowercaseChar(getLastChar(cName))+"Dao;"+RT_1+BLANK_4+"}"+RT_2+"}");
fw.flush();
fw.close();
showInfo(fileName);
}
/**
* 获取路径的最后面字符串br
* 如:br
* codestr = "com.b510.base.bean.User"/codebr
* code return "User";code
* @param str
* @return
*/
public String getLastChar(String str) {
if ((str != null) (str.length() 0)) {
int dot = str.lastIndexOf('.');
if ((dot -1) (dot (str.length() - 1))) {
return str.substring(dot + 1);
}
}
return str;
}
/**
* 把第一个字母变为小写br
* 如:br
* codestr = "UserDao";/codebr
* codereturn "userDao";/code
* @param str
* @return
*/
public String getLowercaseChar(String str){
return str.substring(0,1).toLowerCase()+str.substring(1);
}
/**
* 显示信息
* @param info
*/
public void showInfo(String info){
System.out.println("创建文件:"+ info+ "成功!");
}
/**
* 获取系统时间
* @return
*/
public static String getDate(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return simpleDateFormat.format(new Date());
}
}
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("学生表一");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("学号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("生日");
cell.setCellStyle(style);
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
List list = CreateSimpleExcelToDisk.getStudent();
for (int i = 0; i list.size(); i++)
{
row = sheet.createRow((int) i + 1);
Student stu = (Student) list.get(i);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue((double) stu.getId());
row.createCell((short) 1).setCellValue(stu.getName());
row.createCell((short) 2).setCellValue((double) stu.getAge());
cell = row.createCell((short) 3);
cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu
.getBirth()));
}
// 第六步,将文件存到指定位置
try
{
FileOutputStream fout = new FileOutputStream("E:/students.xls");
wb.write(fout);
fout.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款