本地上传图片到服务器上工具类
Posted 爱编程的欧巴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了本地上传图片到服务器上工具类相关的知识,希望对你有一定的参考价值。
1、工具jar包依赖
<dependency>
<groupId>jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.17</version>
</dependency>
2、代码如下
package com.zbb.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import org.springframework.web.multipart.MultipartFile;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
/**
* 通过SMB协议发送文件到远程服务器
*
*/
public class SMBUtil {
private String url = "";
private SmbFile smbFile = null;
private SmbFileOutputStream smbOut = null;
private static SMBUtil smb = null;
/**
* 得到SMB和连接的方法
* @param url
* @return
*/
public static synchronized SMBUtil getInstance(String url) {
if(smb == null)
return new SMBUtil(url);
return smb;
}
private SMBUtil(String url) {
this.url = url;
this.init();
}
public void init() {
try {
System.out.println("connecting...url:" + this.url);
smbFile = new SmbFile(this.url);
if(!smbFile.exists()){
smbFile.mkdirs();
}
smbFile.connect();
System.out.println("connect successfu...url:" + this.url);
}catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* 上传文件到服务器
* @param file
* @return
*/
public int uploadFile(File file) {
int flag = -1;
BufferedInputStream bf = null;
try{
this.smbOut = new SmbFileOutputStream(this.url + "/" + file.getName(), false);
bf = new BufferedInputStream(new FileInputStream(file));
byte[] bt = new byte[8192];
int n = bf.read(bt);
while (n != -1){
this.smbOut.write(bt, 0, n);
this.smbOut.flush();
n = bf.read(bt);
}
flag = 0;
System.out.println("file transmission complete...");
}catch(SmbException e) {
e.printStackTrace();
}catch(MalformedURLException e) {
e.printStackTrace();
}catch(UnknownHostException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{
try {
if(null != this.smbOut)
this.smbOut.close();
if(null != bf)
bf.close();
}catch(Exception e2) {
e2.printStackTrace();
}
}
return flag;
}
/**
* 上传文件到服务器
* @param file
* @return
*/
public int uploadFile(MultipartFile file,String fileName) {
int flag = -1;
BufferedInputStream bf = null;
try{
this.smbOut = new SmbFileOutputStream(this.url + "/" + fileName, false);
bf = new BufferedInputStream(file.getInputStream());
byte[] bt = new byte[8192];
int n = bf.read(bt);
while (n != -1){
this.smbOut.write(bt, 0, n);
this.smbOut.flush();
n = bf.read(bt);
}
flag = 0;
System.out.println("file transmission complete...");
}catch(SmbException e) {
e.printStackTrace();
}catch(MalformedURLException e) {
e.printStackTrace();
}catch(UnknownHostException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{
try {
if(null != this.smbOut)
this.smbOut.close();
if(null != bf)
bf.close();
}catch(Exception e2) {
e2.printStackTrace();
}
}
return flag;
}
public static void main(String[] args) {
SMBUtil smb = SMBUtil.getInstance("smb://用户名:密码@服务器IP/COMMODITY");
File file = new File("E://服务器信dd息.txt");
smb.uploadFile(file);//上传文件
}
}
3、配置信息
user_name=administrator
user_pwd=akjq55**+6
user_ip=23.234.5.146
pan=d
folder=yhyhImageAddress
以上是关于本地上传图片到服务器上工具类的主要内容,如果未能解决你的问题,请参考以下文章