本文目錄一覽:
java文件上傳到服務器有什麼影響
java文件上傳到服務器沒有什麼影響。存儲方式改變了以及存儲的文件夾會有改變,要注意存儲的文件夾名稱。Java是一種可以撰寫跨平台應用軟件的面向對象的程序設計語言,廣泛應用。
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如何實現文件上傳
public static int transFile(InputStream in, OutputStream out, int fileSize) {
int receiveLen = 0;
final int bufSize = 1000;
try {
byte[] buf = new byte[bufSize];
int len = 0;
while(fileSize – receiveLen bufSize)
{
len = in.read(buf);
out.write(buf, 0, len);
out.flush();
receiveLen += len;
System.out.println(len);
}
while(receiveLen fileSize)
{
len = in.read(buf, 0, fileSize – receiveLen);
System.out.println(len);
out.write(buf, 0, len);
receiveLen += len;
out.flush();
}
} catch (IOException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
return receiveLen;
}
這個方法從InputStream中讀取內容,寫到OutputStream中。
那麼發送文件方,InputStream就是FileInputStream,OutputStream就是Socket.getOutputStream.
接受文件方,InputStream就是Socket.getInputStream,OutputStream就是FileOutputStream。
就OK了。 至於存到數據庫里嘛,Oracle里用Blob。搜索一下,也是一樣的。從Blob能獲取一個輸出流。
java中怎樣上傳文件
Java代碼實現文件上傳
FormFile file=manform.getFile();
String newfileName = null;
String newpathname=null;
String fileAddre=”/numUp”;
try {
InputStream stream = file.getInputStream();// 把文件讀入
String filePath = request.getRealPath(fileAddre);//取系統當前路徑
File file1 = new File(filePath);//添加了自動創建目錄的功能
((File) file1).mkdir();
newfileName = System.currentTimeMillis()
+ file.getFileName().substring(
file.getFileName().lastIndexOf(‘.’));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(filePath + “/”
+ newfileName);
newpathname=filePath+”/”+newfileName;
System.out.println(newpathname);
// 建立一個上傳文件的輸出流
System.out.println(filePath+”/”+file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);// 將文件寫入服務器
}
bos.close();
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
原創文章,作者:FXEJ,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/137307.html