當前位置:首頁 » 文件管理 » java文件上傳遠程伺服器

java文件上傳遠程伺服器

發布時間: 2023-03-15 11:43:30

『壹』 java怎麼實現上傳文件到伺服器

  • 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<ERRORTYPE.LENGTH;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);

  • }

  • }

  • }

  • 現在介紹上傳文件到伺服器,下面只寫出相關代碼:

  • sql2000為例,表結構如下:

  • 欄位名:name filecode

  • 類型: varchar image

  • 資料庫插入代碼為:PreparedStatement pstmt=conn.prepareStatement("insert into test values(?,?)");

  • 代碼如下:

  • 。。。。。。

  • try{

  • 這段代碼如果不去掉,將一同寫入到伺服器中

  • //item.write(new File("d://" + m.group(1)));

  • int byteread=0;

  • //讀取輸入流,也就是上傳的文件內容

  • InputStream inStream=item.getInputStream();

  • pstmt.setString(1,m.group(1));

  • pstmt.setBinaryStream(2,inStream,(int)size);

  • pstmt.executeUpdate();

  • inStream.close();

  • out.println(name+" "+size+" ");

  • }

  • 。。。。。。

  • 這樣就實現了上傳文件至資料庫

『貳』 java 實現文件上傳到另一台伺服器,該怎麼解決

上傳本地文件代碼譽滲源
使用步驟如下:
1.調用AddFile函數添加喊鎮本地文件,注意路徑需要使慶態用雙斜框(\\)
2.調用PostFirst函數開始上傳文件。
JavaScript code?<script type="text/javascript" language="javascript"> var fileMgr = new HttpUploaderMgr(); fileMgr.Load();//載入控制項 window.onload = function() { fileMgr.Init();//初始化控制項 //添加一個本地文件 fileMgr.AddFile("D:\\Soft\\QQ2010.exe"); fileMgr.PostFirst(); };</script>

『叄』 java怎麼將上傳文件到別的伺服器

首先,獲得別的伺服器的上傳介面,然後做寫上傳程序的時候默認設置上傳到該伺服器。或者直接將java上傳程序放在別的伺服器,直接這里調用即可。

『肆』 java怎麼實現上傳附件的功能

上傳附件,實際上就是將文件存儲到遠程伺服器,進行臨時存儲。舉例:
**
* 上傳文件
*
* @param fileName
* @param plainFilePath 文件路徑路徑
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByftp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上傳文件開始");
Log.info("連接遠程上傳伺服器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
}
}
Log.info("檢查文件路徑是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查詢文件路徑不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 設置文件類型(二進制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
Log.info("上傳文件成功:"+fileName+"。文件保存路徑:"+"/"+filepath+"/");
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}
備註:只需要修改上傳的伺服器地址、用戶名、密碼即可進行伺服器訪問上傳。根據實際需要修改即可。

『伍』 java怎麼把文件傳輸到伺服器

String realpath = ServletActionContext.getServletContext().getRealPath("/upload") ;//獲取伺服器路徑
String[] targetFileName = uploadFileName;
for (int i = 0; i < upload.length; i++) {
File target = new File(realpath, targetFileName[i]);
FileUtils.File(upload[i], target);
//這是一個文件復制類File()裡面就是IO操作,如果你不用這個類也可以自己寫一個IO復制文件的類
}

其中private File[] upload;// 實際上傳文件

private String[] uploadContentType; // 文件的內容類型

private String[] uploadFileName; // 上傳文件名

這三個參數必須這樣命名,因為文件上傳控制項默認是封裝了這3個參數的,且在action裡面他們應有get,set方法

『陸』 java上傳圖片到遠程伺服器上,怎麼解決呢

需要這樣的一個包 jcifs-1.1.11
public static void forcdt(String dir){
InputStream in = null;
OutputStream out = null;
File localFile = new File(dir);
try{
//創建file類 傳入本地文件路徑
//獲得本地文件的名字
String fileName = localFile.getName();
//將本地文件的名字和遠程目錄的名字拼接在一起
//確保上傳後的文件於本地文件名字相同
SmbFile remoteFile = new SmbFile("smb://administrator:[email protected]/e$/aa/");
//創建讀取緩沖流把本地的文件與程序連接在一起
in = new BufferedInputStream(new FileInputStream(localFile));
//創建一個寫出緩沖流(注意jcifs-1.3.15.jar包 類名為Smb開頭的類為控制遠程共享計算機"io"包)
//將遠程的文件路徑傳入SmbFileOutputStream中 並用 緩沖流套接
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile+"/"+fileName));
//創建中轉位元組數組
byte[] buffer = new byte[1024];
while(in.read(buffer)!=-1){//in對象的read方法返回-1為 文件以讀取完畢
out.write(buffer);
buffer = new byte[1024];
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
//注意用完操作io對象的方法後關閉這些資源,走則 造成文件上傳失敗等問題。!
out.close();
in.close();
}catch(Exception e){
e.printStackTrace();}
}
}

『柒』 Java怎樣實現跨伺服器文件上傳

另一台機器也要有處理文件上傳的WEB程序,你可以參考Stream上傳插件(支持HTML5和Flash兩種方式上傳)

Stream 上傳插件
Stream 是解決不同瀏覽器上傳文件的插件,是Uploadify的Flash版和Html5版的結合!

Stream 簡介
Stream 是根據某網的文件上傳插件加工而來,支持不同平台(Windows, Linux, Mac, Android, iOS)下,主流瀏覽器(IE7+, Chrome, Firefox, Safari, 其他)的上傳工作,當然在Html5標准下,還支持文件的斷點續傳功能,有效解決大文件的Web上傳問題!

主要特徵
1. 源碼完全開放,目前有Java、PHP、Perl三種後台語言實現

2. 支持HTML5、Flash兩種方式(跨域)上傳

3. 多文件一起上傳

4. HTML5支持斷點續傳,拖拽等新特性

5. 兼容性好IE7+, FF3.6+, Chrome*,Safari4+,遨遊等主流瀏覽器

6. 進度條、速度、剩餘時間等附屬信息

7. `選擇文件的按鈕`可以自定義

8. 簡單的參數配置實現 靈活多變的功能

9. 支持文件夾上傳(Chrome21+, Opera15+)

10. 支持自定義UI(V1.4+)

指定跨域上傳就可以了

『捌』 java中怎麼把文件上傳到伺服器的指定路徑

文件從本地到伺服器的功能,其實是為了解決目前瀏覽器不支持獲取本地文件全路徑。不得已而想到上傳到伺服器的固定目錄,從而方便項目獲取文件,進而使程序支持EXCEL批量導入數據。

java中文件上傳到伺服器的指定路徑的代碼:

在前台界面中輸入:

<form method="post" enctype="multipart/form-data" action="../manage/excelImport.do">

請選文件:<input type="file" name="excelFile">

<input type="submit" value="導入" onclick="return impExcel();"/>

</form>

action中獲取前台傳來數據並保存

/**

* excel 導入文件明頌

* @return

* @throws IOException

*/

@RequestMapping("/usermanager/excelImport.do")

public String excelImport(

String filePath,

MultipartFile excelFile,HttpServletRequest request) throws IOException{

log.info("<<悉孫<<<<action:{} Method:{} start>>>>>>","usermanager","excelImport" );

if (excelFile != null){

String filename=excelFile.getOriginalFilename();

String a=request.getRealPath("u/cms/www/201509");

SaveFileFromInputStream(excelFile.getInputStream(),request.getRealPath("u/cms/www/201509"),filename);//保存到服激陸鄭務器的路徑

}

log.info("<<<<<<action:{} Method:{} end>>>>>>","usermanager","excelImport" );

return "";

}

/**

* 將MultipartFile轉化為file並保存到伺服器上的某地

*/

public void SaveFileFromInputStream(InputStream stream,String path,String savefile) throws IOException

{

FileOutputStream fs=new FileOutputStream( path + "/"+ savefile);

System.out.println("------------"+path + "/"+ savefile);

byte[] buffer =new byte[1024*1024];

int bytesum = 0;

int byteread = 0;

while ((byteread=stream.read(buffer))!=-1)

{

bytesum+=byteread;

fs.write(buffer,0,byteread);

fs.flush();

}

fs.close();

stream.close();

}

『玖』 Java怎麼上傳文件到遠程Windows伺服器

安裝一個FTP就可以直接上傳下載了,你可以去伺服器廠商(正睿)的網上找找相關技術文檔參考一下,很快就清楚了!

『拾』 javaweb 怎麼樣將本地文件傳輸到遠程伺服器

可以通過JDK自帶的API實現,如下代碼:
package com.cloudpower.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

/**
* Java自帶的API對FTP的操作
* @Title:Ftp.java
*/
public class Ftp {
/**
* 本地文件名
*/
private String localfilename;
/**
* 遠程文件名
*/
private String remotefilename;
/**
* FTP客戶端
*/
private FtpClient ftpClient;

/**
* 伺服器連接
* @param ip 伺服器IP
* @param port 伺服器埠
* @param user 用戶名
* @param password 密碼
* @param path 伺服器路徑
* @date 2012-7-11
*/
public void connectServer(String ip, int port, String user,
String password, String path) {
try {
/* ******連接伺服器的兩種方法*******/
//第一種方法
// ftpClient = new FtpClient();
// ftpClient.openServer(ip, port);
//第二種方法
ftpClient = new FtpClient(ip);

ftpClient.login(user, password);
// 設置成2進制傳輸
ftpClient.binary();
System.out.println("login success!");
if (path.length() != 0){
//把遠程系統上的目錄切換到參數path所指定的目錄
ftpClient.cd(path);
}
ftpClient.binary();
} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
public void closeConnect() {
try {
ftpClient.closeServer();
System.out.println("disconnect success");
} catch (IOException ex) {
System.out.println("not disconnect");
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
public void upload(String localFile, String remoteFile) {
this.localfilename = localFile;
this.remotefilename = remoteFile;
TelnetOutputStream os = null;
FileInputStream is = null;
try {
//將遠程文件加入輸出流中
os = ftpClient.put(this.remotefilename);
//獲取本地文件的輸入流
File file_in = new File(this.localfilename);
is = new FileInputStream(file_in);
//創建一個緩沖區
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
System.out.println("upload success");
} catch (IOException ex) {
System.out.println("not upload");
ex.printStackTrace();
throw new RuntimeException(ex);
} finally{
try {
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void download(String remoteFile, String localFile) {
TelnetInputStream is = null;
FileOutputStream os = null;
try {
//獲取遠程機器上的文件filename,藉助TelnetInputStream把該文件傳送到本地。
is = ftpClient.get(remoteFile);
File file_in = new File(localFile);
os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
System.out.println("download success");
} catch (IOException ex) {
System.out.println("not download");
ex.printStackTrace();
throw new RuntimeException(ex);
} finally{
try {
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static void main(String agrs[]) {

String filepath[] = { "/temp/aa.txt", "/temp/regist.log"};
String localfilepath[] = { "C:\\tmp\\1.txt","C:\\tmp\\2.log"};

Ftp fu = new Ftp();
/*
* 使用默認的埠號、用戶名、密碼以及根目錄連接FTP伺服器
*/
fu.connectServer("127.0.0.1", 22, "anonymous", "IEUser@", "/temp");

//下載
for (int i = 0; i < filepath.length; i++) {
fu.download(filepath[i], localfilepath[i]);
}

String localfile = "E:\\號碼.txt";
String remotefile = "/temp/哈哈.txt";
//上傳
fu.upload(localfile, remotefile);
fu.closeConnect();
}
}

熱點內容
鳥存儲空氣 發布:2025-05-18 00:20:24 瀏覽:200
linux刻錄iso 發布:2025-05-18 00:16:15 瀏覽:661
php動態參數 發布:2025-05-18 00:12:05 瀏覽:425
安卓應用上傳 發布:2025-05-18 00:11:57 瀏覽:802
數對的演算法 發布:2025-05-18 00:11:02 瀏覽:381
linuxwhile 發布:2025-05-18 00:10:08 瀏覽:143
xpftp外網 發布:2025-05-17 23:58:11 瀏覽:385
如何評價一個伺服器的性能 發布:2025-05-17 23:40:53 瀏覽:270
淘寶客適合什麼伺服器 發布:2025-05-17 23:39:26 瀏覽:613
python循環文件 發布:2025-05-17 23:39:22 瀏覽:828