javasmb上传文件
‘壹’ java读取linux系统中文文件名时候乱码,并显示文件不存在
同学,这个很麻烦的,大哥解决方案就是存文件加码,取文件解码。用base64这个东西,你可以网络一下。然后在linux下就不存在有中文文件了。
‘贰’ 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如何访问局域网共享文件
java访问共享文件夹,读取局域网中一台机器的共享目录中的文件,需要jcifs-1.1.11.jar的支持,使用SMB协议,以下是实现了远程读取文件的功能代码:
packagejunit;
importjcifs.smb.SmbFile;
/**
*java访问局域网共享目录
*
*@authoradministrator
*@version1.02015-7-6
*/
publicclassSmbTest{
publicstaticvoidmain(String[]args)throwsException{
//smb://xxx:[email protected]/testIndex/
//xxx:xxx是共享机器的用户名密码
Stringurl="smb://192.168.2.188/testIndex/";
SmbFilefile=newSmbFile(url);
if(file.exists()){
SmbFile[]files=file.listFiles();
for(SmbFilef:files){
System.out.println(f.getName());
}
}
}
}