java 文件断点续传

public static boolean fileUpload(String uuid,File file,SimpleDateFormat dateFormat,String filename){
		try{
			HttpURLConnection conn=HttpRequestUtils.getUrlConnection(Constants.FILE_SERVER, Constants.BUFFER_SIZE);
			conn.setRequestProperty("type","1");
			conn.setRequestProperty("source", "s1");
			conn.setRequestProperty("range", String.valueOf(0));
			conn.setRequestProperty("filename", filename);
			conn.setRequestProperty("filecreatetime", dateFormat.format(new Date()));
			conn.setRequestProperty("fileid", uuid);
			conn.setRequestProperty("totalsize", String.valueOf(file.length()));
			
			RandomAccessFile randomAccessFile=new RandomAccessFile(file, "rw");
			OutputStream out = new DataOutputStream(conn.getOutputStream());
			
			int j=0;
			byte[] buf = new byte[Constants.BUFFER_SIZE];
			int nRead=0;
			try {
				randomAccessFile.seek(Long.valueOf(0));//设置文件指针
				while((nRead=randomAccessFile.read(buf,0,Constants.BUFFER_SIZE)) > 0)
				{
					out.write(buf,0,nRead);
					j += nRead;
				}
				out.close();
				randomAccessFile.close();
			} catch (NumberFormatException e) {
				e.printStackTrace();
				return false;
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}finally{
				try {
					randomAccessFile.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
				
			 	out.flush();
			 	out.close();
			 	// 定义BufferedReader输入流来读取URL的响应
			 	BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
			 	String line = null;
			 	while ((line = reader.readLine()) != null) {
			 		System.out.println(line);
			 	}
		}catch(Exception e){
			logger.error(e);
			e.printStackTrace();
			return false;
		}
		
		 	
		return true;
	}

 下面是接收的部分代码:

response.reset();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		//统一获取请求头的参数
		String range  =  request.getHeader("range");
		String totalsize = request.getHeader("totalsize");
		String fileid = request.getHeader("fileid");
		String filecreatetime = request.getHeader("filecreatetime");
		String filename = request.getHeader("filename");
		String source=request.getHeader("source");


File destfile=new File(storeFilePath+File.separator+filename);
		File descfile=new File(storeFilePath);
		descfile.mkdirs();

logger.info("文件地址:" + destfile);
		if(!destfile.exists()){
			try {
				destfile.createNewFile();
			} catch (IOException e) {
				logger.error(e);
				fileUploadResult = new FileUploadResult(false,FileBizCodeConstants.FILEID_UNEXIST, "IO读写出错");
				return fileUploadResult;
			}
		}

long filesize=destfile.length();
		RandomAccessFile randomAccessFile;
		try {
			randomAccessFile = new RandomAccessFile(destfile, "rw");
		} catch (FileNotFoundException e) {
			logger.error(e);
			fileUploadResult = new FileUploadResult(false,FileBizCodeConstants.FILEID_UNEXIST, "文件未找到");
			return fileUploadResult;
		}


byte[] buf = new byte[FileBizCodeConstants.BUFFER_SIZE];
		int nRead;
		int stu=0;


try {
			
			randomAccessFile.seek(Long.valueOf(range));//设置文件指针
			while((nRead=is.read(buf,0,FileBizCodeConstants.BUFFER_SIZE)) > 0&&stu==0)
			{
				randomAccessFile.write(buf,0,nRead);
				l += nRead;
			}
		}

猜你喜欢

转载自shendixiong.iteye.com/blog/2226134