文件续传的java代码 java文件传输系统

关于JAVA断点续传024字节) 。第一次B接收文件续传的java代码了512字节 , 那么第二次连接A就应该从513字节开始传输 。
也就是说,在第二次传输时 , B要提供“文件续传的java代码我要从513字节开始传送文件F”的信息,然后A使用FileInputStream构建输入流读取本地文件 , 使用skip(512)方法跳过文件F的前512字节再传送文件,之后B将数据追加(append)到先前接收的文件末尾即可 。
进一步考虑,如果要实现多线程传送,即分块传输,也同样的道理 。假如B要求分作两块同时传输,那么A启动两个线程,一个从513字节读到768字节(工256字节),第二个线程从769字节到1024字节即可 。
如果文件续传的java代码你要从网络上下载文件,就是说A方不是你实现的,那么你要先确认A方支不支持断电续传功能(HTTP1.1) , 然后你查阅下HTTP1.1协议,在HTTP1.1版本里,可以通过设置请求包头某个字段的信息(使用URLConnection创建连接并使用setRequestProperty(String key, String value) 方法设置)从而精确读取文件的某一段数据的 。注意,基于HTTP断点续传的关键是1.1版本 , 1.0版本是不支持的 。
补充文件续传的java代码:
嗯,查到了,是设置range属性,即setRequestProperty("range", "bytes=513-1024").你可以使用迅雷下载某个文件,然后从”线程信息“中就可以看到这个http1.1断点续传的所有行为信息了 。
java web断点续传,我用的是fileupload来做的上传 。使用Struts2上传文件文件续传的java代码:
Struts文件上传需要使用File Upload Filter 。Filter Upload Filter使用一些默认文件续传的java代码的规则:
Form中的s:file name="image"/s:file标签对应着Action类中的三个属性分别是:上传文件(java.io.File类型)文件续传的java代码,文件名(java.lang.String类型)文件续传的java代码,文件类型(java.lang.String类型文件续传的java代码,例如:image/jpeg) 。命名规约为:
文件:名字与s:file标签中的name属性一致,这里为:image
文件名:文件FileName,这里为:imageFileName
文件类型:文件ContentType,这里为:imageContentType
所以针对上述s:file name="image"/s:file表示啊的上传文件的JSP和Action类被别为:
imageUpload.jsp:
[html] view plain copy
%@ page contentType="text/html;charset=UTF-8" language="java" %
%@taglib prefix="s" uri="/struts-tags" %
html
headtitleImage Upload/title/head
body
h1 Image Upload Page /h1
s:form action="imageUpload" method="post" enctype="multipart/form-data"
s:file name="image"/s:file
s:submit/s:submit
/s:form
/body
/html
ImageUploadAction.java:
[html] view plain copy
package com.jpleasure;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class ImageUploadAction extends ActionSupport {
private File image;
private String imageFileName;
private String imageContentType;
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
public String execute() {
if (image != null) {
System.out.println("file name is:"this.imageFileName);
System.out.println("file content type is:"this.imageContentType);
System.out.println("file length is:"this.image.length());
}
return SUCCESS;
}
}
Struts.xml配置文件:
[html] view plain copy
action name="imageUpload" class="com.jpleasure.ImageUploadAction"
result/success.jsp/result
/action
这样当我们选中上传文件,提交的时候:文件内容会以File类型的方式放在image声明的变量中 。文件的名字将会被放在imageFileName命名的变量中,文件的类型被放在imageContentType命名的变量中 。
文件下载:
文件下载需要使用一个特殊的Result,stream类型的Result 。Stream类型的Result主要用来处理文件下载操作 。
处理原理为:所有的下载文件都是将一个二进制的流写入到HttpResponse中去 。在Action类中定义一个InputSream类型的二进制流,在Result返回给用户的时候返回给用户 。
扩展上述的代码,将上传来的文件直接下载给用户:
ImageUploadAction中需要追加一个InputSream类型的对象,并且指向上传的文件,代码如下 , 红色部分表示变化:
[html] view plain copy
package com.jpleasure;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class ImageUploadAction extends ActionSupport {
private File image;
private String imageFileName;
private String imageContentType;
private InputStream imageInputStream = null;
public InputStream getImageInputStream() {
return imageInputStream;
}
public void setImageInputStream(InputStream imageInputStream) {
this.imageInputStream = imageInputStream;
}
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
public String execute() {
if (image != null) {
System.out.println("file name is:"this.imageFileName);
System.out.println("file content type is:"this.imageContentType);
System.out.println("file length is:"this.image.length());
try {
this.imageInputStream = new FileInputStream (image);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return SUCCESS;
}
}
配置文件为,红色为变化部分:
[html] view plain copy
action name="imageUpload" class="com.jpleasure.ImageUploadAction"
result name="success" type="stream"
param name="contentType"image/pjpeg/param
param name="inputName"imageInputStream/param
param name="contentDisposition"attachment;filename="image.jpg"/param
param name="bufferSize"1024/param
/result
/action
ContentType表示下载文件的类型 。
InputName表示Action类中用来下载文件的字段的名字 。
ContentDisposition用来控制文件下载的一些信息 , 包括是否打开另存对话框 , 下载文件名等 。
BufferSize表示文件下载时使用的缓冲区的大小 。
实际项目开发的考虑:
控制上传文件的类型和最大允许上传文件的size
使用File Upload Intercepter的参数可盈控制上传文件的类型和最大允许上传文件的size 。例如:
[html] view plain copy
struts
package name="myPackage" extends="struts-default"
interceptor-ref name="fileUpload"
param name="maximumSize"2MB/param
param name="allowedTypes"text/html,image/jpeg/param
/interceptor-ref
interceptor-ref name="basicStack"/
action name="imageUpload" class="com.jpleasure.ImageUploadAction"
result name="success" type="stream"
param name="contentType"image/pjpeg/param
param name="inputName"imageInputStream/param
param name="contentDisposition"
attachment;filename="image.jpg"
/param
param name="bufferSize"1024/param
/result
/action
/package
/struts
上述表示允许上传jpeg和html类型的文件 , 且最大文件上传size为2MB
显示错误信息:
可以使用如下key表示的message来显示文件上传出错的提示信息:
消息Key说明
struts.messages.error.uploading文件无法正常上传时的公共错误
struts.messages.error.file.too.large文件大小超过最大允许size时的错误提示
struts.messages.error.content.type.not.allowed文件类型不在上传文件允许类型中的错误提示
Java Socket如何实现文件的断点续传,有代码更好1package com.tangshun.;
【文件续传的java代码 java文件传输系统】2
3import java.io.File;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.RandomAccessFile;
7import java.net.HttpURLConnection;
8import java.net.MalformedURLException;
9import java.net.URL;
10
11//断点续传
12public class DownLoad {
13
14public static void down(String URL, long nPos, String savePathAndFile) {
15try {
16URL url = new URL(URL);
17HttpURLConnection httpConnection = (HttpURLConnection) url
18.openConnection();
19// 设置User-Agent
20httpConnection.setRequestProperty("User-Agent", "NetFox");
21// 设置断点续传的开始位置
22httpConnection.setRequestProperty("RANGE", "bytes="nPos);
23// 获得输入流
24InputStream input = httpConnection.getInputStream();
25RandomAccessFile oSavedFile = new RandomAccessFile(savePathAndFile,
26"rw");
27// 定位文件指针到nPos位置
28oSavedFile.seek(nPos);
29byte[] b = new byte[1024];
30int nRead;
31// 从输入流中读入字节流,然后写到文件中
32while ((nRead = input.read(b, 0, 1024))0) {
33(oSavedFile).write(b, 0, nRead);
34}
35httpConnection.disconnect();
36} catch (MalformedURLException e) {
37e.printStackTrace();
38} catch (IOException e) {
39e.printStackTrace();
40}
41}
42
43public static long getRemoteFileSize(String url) {
44long size = 0;
45try {
46HttpURLConnection conn = (HttpURLConnection) (new URL(url))
47.openConnection();
48size = conn.getContentLength();
49conn.disconnect();
50} catch (Exception e) {
51e.printStackTrace();
52}
53return size;
54}
55
56public static void main(String[] args) {
57 String url = " ";
58String savePath = "F:\\";
59String fileName = url.substring(url.lastIndexOf("/"));
60String fileNam=fileName;
61HttpURLConnection conn = null;
62try {
63conn = (HttpURLConnection) (new URL(url)).openConnection();
64} catch (Exception e) {
65e.printStackTrace();
66}
67File file = new File(savePathfileName);
68// 获得远程文件大小
69long remoteFileSize = getRemoteFileSize(url);
70System.out.println("远程文件大小=" remoteFileSize);
71int i = 0;
72if (file.exists()) {
73// 先看看是否是完整的 , 完整,换名字,跳出循环,不完整,继续下载
74long localFileSize = file.length();
75System.out.println("已有文件大小为:" localFileSize);
76
77if (localFileSizeremoteFileSize) {
78System.out.println("文件续传");
79down(url, localFileSize, savePathfileName);
80}else{
81System.out.println("文件存在 , 重新下载");
82do{
83i;
84fileName = fileNam.substring(0, fileNam.indexOf("."))"("i
85")"fileNam.substring(fileNam.indexOf("."));
86
87file = new File(savePathfileName);
88}while(file.exists());
89try {
90file.createNewFile();
91} catch (IOException e) {
92e.printStackTrace();
93}
94down(url, 0, savePathfileName);
95}
96// 下面表示文件存在,改名字
97
98} else {
99try {
100file.createNewFile();
101System.out.println("下载中");
102down(url, 0, savePathfileName);
103} catch (IOException e) {
104e.printStackTrace();
105}
106}
107}}
108
文件续传的java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java文件传输系统、文件续传的java代码的信息别忘了在本站进行查找喔 。

    推荐阅读