導航:首頁 > 編程語言 > java上傳文件伺服器

java上傳文件伺服器

發布時間:2022-08-25 12:51:10

A. 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方法

B. JAVA如何把本地文件上傳到伺服器。

這個你就要參考,java上傳文件的相關資料了。
告訴你有現成的代碼,你可以找寫拷貝過去直接使用。

C. java怎麼實現上傳文件到伺服器

D. 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上傳文件伺服器相關的資料

熱點內容
阿里雲伺服器終端在哪裡 瀏覽:144
app紙有什麼用 瀏覽:219
cuteftp命令 瀏覽:502
最開始的編程語言是什麼 瀏覽:757
at遠程命令 瀏覽:490
雲伺服器哪家好點 瀏覽:211
android系統源碼閱讀 瀏覽:924
dumpjava分析工具 瀏覽:678
怎麼下載cpu源碼 瀏覽:154
代碼加密怎麼取消 瀏覽:888
編譯原理代碼在哪裡運行 瀏覽:584
解密攝影pdf 瀏覽:72
演算法編程中級題目 瀏覽:250
c語言編譯器畢業設計 瀏覽:717
醫保卡申請app哪個好 瀏覽:945
阿里雲伺服器上傳源碼 瀏覽:602
營銷管理科特勒pdf 瀏覽:696
願望清單app哪個好 瀏覽:461
安卓外放聲音怎麼解決 瀏覽:195
脈脈app干什麼用的 瀏覽:361