導航:首頁 > 編程語言 > java傳輸大文件

java傳輸大文件

發布時間:2022-08-21 21:13:16

java如何快速復制大文件

我希望從本地和遠程復制文件,文件都很大,10G級的,如何快速的復制?看網上有人說使用管道到管道,這確實比其他的快,不知道fast是怎樣實現的 本地遠程復制大文件啊,而且還是10G這么大的。你看看迅雷啊,他是多部分一起傳的。Java傳輸文件就是一般的文件操作啊,你可以模仿迅雷,開多個線程下載,每個線程記錄下載部分的開始和結束索引,一起下到遠程去。至於效率有多高,Java我還真不敢說,要不迅雷,快車這種工具怎麼不用Java來開發。 用FileChannel的transferTo(long position, long count, WritableByteChannel target)方法試試用FileChannel的transferTo(long position, long count, WritableByteChannel target)方法試試 public static void main(String[] args)throws Exception{ FileInputStream fileIn=new FileInputStream("d:/JavaTests/TestFileChannel.txt"); FileOutputStream fileOut=new FileOutputStream("d:/JavaTests/TestFileOutputStreamCopyTime.txt");int i;long l=System.currentTimeMillis(); while((i=fileIn.read())!=-1){ fileOut.write(i);}System.out.println("Take time:"+(System.currentTimeMillis()-l)+"ms");//516ms fileIn.close(); 本地遠程復制大文件啊,而且還是10G這么大的。你看看迅雷啊,他是多部分一起傳的。Java傳輸文件就是一般的文件操作啊,你可以模仿迅雷,開多個線程下載,每個線程記錄下載部分的開始和結束索引,一起下到遠程去。

㈡ java ftp上傳5G以上大文件,怎麼做

java上傳可以使用common-fileupload上傳組件的。common-fileupload是jakarta項目組開發的一個功能很強大的上傳文件組件下面先介紹上傳文件到伺服器(多文件上傳):import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import org.apache.commons.fileupload.*;
public class upload extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GB2312";
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out=response.getWriter();
try {
DiskFileUpload fu = new DiskFileUpload();
// 設置允許用戶上傳文件大小,單位:位元組,這里設為2m
fu.setSizeMax(2*1024*1024);
// 設置最多隻允許在內存中存儲的數據,單位:位元組
fu.setSizeThreshold(4096);
// 設置一旦文件大小超過getSizeThreshold()的值時數據存放在硬碟的目錄
fu.setRepositoryPath("c:\\windows\\temp");
//開始讀取上傳信息
List fileItems = fu.parseRequest(request);
// 依次處理每個上傳的文件
Iterator iter = fileItems.iterator();//正則匹配,過濾路徑取文件名
String regExp=".+\\\\(.+)$";//過濾掉的文件類型
String[] errorType={".exe",".com",".cgi",".asp"};
Pattern p = Pattern.compile(regExp);
while (iter.hasNext()) {
FileItem item = (FileItem)iter.next();
//忽略其他不是文件域的所有表單信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result){
for (int temp=0;temp if (m.group(1).endsWith(errorType[temp])){
throw new IOException(name+": wrong type");
}
}
try{//保存上傳的文件到指定的目錄//在下文中上傳文件至資料庫時,將對這里改寫
item.write(new File("d:\\" + m.group(1))); out.print(name+" "+size+"
");
}
catch(Exception e){
out.println(e);
} }
else
{
throw new IOException("fail to upload");
}
}
}
}
catch (IOException e){
out.println(e);
}
catch (FileUploadException e){
out.println(e);
}

}
}

㈢ java通過sftp上傳大文件,時間長,而且會提示超出GC開銷限制,內存溢出,這種問題怎麼解決

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
HttpSession session = (HttpSession) request.getSession();
final long MAX_SIZE = 10 * 1024 * 1024;// 設置上傳文件最大為 10M
// 允許上傳的文件格式的列表
final String[] allowedExt = new String[] { "jpg", "jpeg", "gif", "png",
"JPG", "bmp", "BMP" };
response.setContentType("text/html;charset=gbk");
// 設置字元編碼為UTF-8, 這樣支持漢字顯示
response.setCharacterEncoding("GBK");
String strImageName = (String) session.getAttribute("strName");
if (ServletFileUpload.isMultipartContent(request)) {
// 實例化一個硬碟文件工廠,用來配置上傳組件ServletFileUpload
DiskFileItemFactory dfif = new DiskFileItemFactory(); dfif.setSizeThreshold(4096);// 設置上傳文件時用於臨時存放文件的內存大小,這里是4K.多於的部分將臨時存在硬碟
dfif.setRepository(new File(this.getServletContext().getRealPath(
"/")
+ "Image"));// 設置存放臨時文件的目錄,web根目錄下的Image目錄
// 用以上工廠實例化上傳組件
ServletFileUpload sfu = new ServletFileUpload(dfif); // 設置最大上傳尺寸
sfu.setSizeMax(MAX_SIZE); PrintWriter out = response.getWriter();
// 從request得到 所有 上傳域的列表
List fileList = null;
try {
fileList = sfu.parseRequest(request);
} catch (FileUploadException e) {// 處理文件尺寸過大異常
if (e instanceof SizeLimitExceededException) {
out.println("文件尺寸超過規定大小:" + MAX_SIZE + "位元組<p />");
out.println("<a href='addGoods.jsp' >返回</a>");
return;
}
e.printStackTrace();
}
// 沒有文件上傳
if (fileList == null || fileList.size() == 0) {
out.println("請選擇要上傳文件a<p />");
out.println("<a href='addGoods.jsp' >返回</a>");
return;
}
// 得到所有上傳的文件
Iterator fileItr = fileList.iterator();
// 循環處理所有文件
this.list = new ArrayList();
while (fileItr.hasNext()) {
long size = 0;
// 得到當前文件
fileItem = (FileItem) fileItr.next();
// 忽略簡單form欄位而不是上傳域的文件域(<input type="text" />等)
if (fileItem == null || fileItem.isFormField()) {
System.out.println(fileItem.getFieldName());
inputstr = fileItem.getString("GBK");
list.add(inputstr);
continue;
}
// 得到文件的完整路徑
path = fileItem.getName();
// 得到文件的大小
size = fileItem.getSize();
if ("".equals(path) || size == 0) {
out.println("請選擇上傳文件<p />");
out.println("<a href='addGoods.jsp' >返回</a>");
return;
}
System.out.println("文件的完整路徑" + path);
// 得到去除路徑的文件名
t_name = path.substring(path.lastIndexOf("\\") + 1);
// 得到文件的擴展名(無擴展名時將得到全名)
String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
// 拒絕接受規定文件格式之外的文件類型
//System.out.println("文件名:" + t_name);
//System.out.println("文件擴展名:"+t_ext);
// System.out.println(t_ext);
int allowFlag = 0;
int allowedExtCount = allowedExt.length;
for (; allowFlag < allowedExtCount; allowFlag++) {
if (allowedExt[allowFlag].equals(t_ext))
break;
}
if (allowFlag == allowedExtCount) {
out.println("請上傳以下類型的文件<p />");
for (allowFlag = 0; allowFlag < allowedExtCount; allowFlag++)
out.println("*." + allowedExt[allowFlag]
+ " ");
out.println("<p /><a href='addGoods.jsp' >返回</a>");
return;
}
long now = System.currentTimeMillis();
// 根據系統時間生成上傳後保存的文件名
u_name = this.getServletContext().getRealPath("/")
+ "ImageDown\\" + t_name;
//System.out.println(u_name);
try {
// 保存文件
fileItem.write(new File(u_name));
/*out
.println("文件上傳成功. 文件大小: " + size
+ "位元組<p />");
out.println("圖片上傳成功!"
+ "<a href='addGoods.jsp' >繼續添加商品</a>");*/
} catch (Exception e) {
e.printStackTrace();
}
}

㈣ Java 批量大文件上傳下載如何實現

解決這種大文件上傳不太可能用web上傳的方式,只有自己開發插件或是當門客戶端上傳,或者用現有的ftp等。
1)開發一個web插件。用於上傳文件。
2)開發一個FTP工具,不用web上傳。
3)用現有的FTP工具。
下面是幾款不錯的插件,你可以試試:
1)Jquery的uploadify插件。具體使用。你可以看幫助文檔。

㈤ java實現大文件上傳

使用Apache的上傳組件。只要採用好的演算法應該都不成問題。
或者編寫一個客戶端,使用socket進行傳輸

㈥ 如何使用java實現基於Http協議的大文件傳輸

雖然在JDK的java.net包中已經提供了訪問HTTP協議的基本功能,但是對於大部分應用程序來說,JDK庫本身提供的功能還不夠豐富和靈活。HttpClient是ApacheJakartaCommon下的子項目,用來提供高效的、最新的、功能豐富的支持HTTP協議的客戶端編程工具包,並且它支持HTTP協議最新的版本和建議。以下是簡單的post例子:Stringurl="bbslogin2.php";PostMethodpostMethod=newPostMethod(url);//填入各個表單域的值NameValuePair[]data={newNameValuePair("id","youUserName"),newNameValuePair("passwd","yourPwd")};//將表單的值放入postMethod中postMethod.setRequestBody(data);//執行postMethodintstatusCode=httpClient.executeMethod(postMethod);//HttpClient對於要求接受後繼服務的請求,象POST和PUT等不能自動處理轉發//301或者302if(statusCode==HttpStatus.SC_MOVED_PERMANENTLY||statusCode==HttpStatus.SC_MOVED_TEMPORARILY){//從頭中取出轉向的地址HeaderlocationHeader=postMethod.getResponseHeader("location");Stringlocation=null;if(locationHeader!=null){location=locationHeader.getValue();System.out.println("Thepagewasredirectedto:"+location);}else{System.err.println("Locationfieldvalueisnull.");}return;}詳情見:/developerworks/cn/opensource/os-httpclient/

㈦ JAVA 前端大文件上傳如何實現

如果是頁面傳至後台,那麼頁面上傳文件的時候進行分片處理,如果是後台之間調用,直接代理裡面分片處理。關鍵點,當前一片傳輸完畢之後,收到成功返回才開始傳輸下一片如果傳輸失敗則重傳當前片數,超時重傳。傳輸時帶上文件唯一標識,文件當前片數,總片數,當前片數md5校驗值等。必要參數。 大致為這樣,注意不要內存泄露了。

閱讀全文

與java傳輸大文件相關的資料

熱點內容
度人經pdf 瀏覽:898
怎麼配置android遠程伺服器地址 瀏覽:956
java程序員看哪些書 瀏覽:939
什麼app可以免費和外國人聊天 瀏覽:793
pdf手寫筆 瀏覽:178
別永遠傷在童年pdf 瀏覽:986
愛上北斗星男友在哪個app上看 瀏覽:419
主力散戶派發源碼 瀏覽:668
linux如何修復伺服器時間 瀏覽:59
榮縣優途網約車app叫什麼 瀏覽:477
百姓網app截圖是什麼意思 瀏覽:226
php如何嵌入html 瀏覽:813
解壓專家怎麼傳輸 瀏覽:745
如何共享伺服器的網路連接 瀏覽:134
程序員簡易表白代碼 瀏覽:168
什麼是無線加密狗 瀏覽:64
國家反詐中心app為什麼會彈出 瀏覽:69
cad壓縮圖列印 瀏覽:104
網頁打開速度與伺服器有什麼關系 瀏覽:865
android開發技術文檔 瀏覽:65