Ⅰ 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文件夾上傳解決方案
必須使用插件,單純瀏覽器無法實現。
Ⅲ java 實現ftp上傳如何創建文件夾
准備條件:java實現ftp上傳用到了commons-net-3.3.jar包
首先建立ftphost連接
publicbooleanconnect(Stringpath,Stringaddr,intport,Stringusername,Stringpassword){
try{
//FTPClientftp=newFTPHTTPClient(addr,port,username,password);
ftp=newFTPClient();
intreply;
ftp.connect(addr);
System.out.println("連接到:"+addr+":"+port);
System.out.print(ftp.getReplyString());
reply=ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)){
ftp.disconnect();
System.err.println("FTP目標伺服器積極拒絕.");
System.exit(1);
returnfalse;
}else{
ftp.login(username,password);
ftp.enterLocalPassiveMode();
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.changeWorkingDirectory(path);
System.out.println("已連接:"+addr+":"+port);
returntrue;
}
}catch(Exceptionex){
ex.printStackTrace();
System.out.println(ex.getMessage());
returnfalse;
}
}
然後再利用ftpclient的makeDirectory方法創建文件夾
publicvoidcreateDir(Stringdirname){
try{
ftp.makeDirectory(dirname);
System.out.println("在目標伺服器上成功建立了文件夾:"+dirname);
}catch(Exceptionex){
System.out.println(ex.getMessage());
}
}
斷開host連接
publicvoiddisconnect(){
try{
ftp.disconnect();
}catch(IOExceptione){
e.printStackTrace();
}
}
最後是程序的調用方法
publicstaticvoidmain(String[]args){
FtpUploadTestftpupload=newFtpUploadTest();
if(ftpupload.connect("","172.39.8.x",20,"administrator","abc@123")){
ftpupload.createDir("/UPLOAD");
ftpupload.disconnect();
}
}
Ⅳ java中怎樣上傳文件
Java代碼實現文件上傳
FormFilefile=manform.getFile();
StringnewfileName=null;
Stringnewpathname=null;
StringfileAddre="/numUp";
try{
InputStreamstream=file.getInputStream();//把文件讀入
StringfilePath=request.getRealPath(fileAddre);//取系統當前路徑
Filefile1=newFile(filePath);//添加了自動創建目錄的功能
((File)file1).mkdir();
newfileName=System.currentTimeMillis()
+file.getFileName().substring(
file.getFileName().lastIndexOf('.'));
ByteArrayOutputStreambaos=newByteArrayOutputStream();
OutputStreambos=newFileOutputStream(filePath+"/"
+newfileName);
newpathname=filePath+"/"+newfileName;
System.out.println(newpathname);
//建立一個上傳文件的輸出流
System.out.println(filePath+"/"+file.getFileName());
intbytesRead=0;
byte[]buffer=newbyte[8192];
while((bytesRead=stream.read(buffer,0,8192))!=-1){
bos.write(buffer,0,bytesRead);//將文件寫入伺服器
}
bos.close();
stream.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
Ⅳ java怎麼讀取文件夾中的內容並上傳
FileInputStream fis = new FileInputStream("d:/a.txt");//從a.txt中讀出
FileOutputStream fos = new FileOutputStream("d:/b.txt");//寫到b.txt中去
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(fos));
String temp;
while((temp = reader.readLine())!= null){//一次讀一行
write.write(temp);
}
reader.close();
write.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如何上傳本地文件夾到其他伺服器(不一定是FTP伺服器)
試試smartupload,網上有例子,很多,,,
Ⅷ 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 WEB項目文件夾上傳下載方法
兩種實現方式,一種是藉助FTP伺服器實現上傳下載,引入相應的jar包,直接拷貝網上現成的代碼,另一種通過原生的代碼,讀取文件夾及裡面的文件,通過io流處理,存放到指定地址,或資料庫設計一個大欄位,存放二進制流數據