java上传图片到远程服务器上,怎么解决呢?

如题所述

第1个回答  推荐于2016-10-04
不好实现,网上有方法说用FTP,但是不会用啊,找了一个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();本回答被提问者采纳
相似回答