项目pom.xml中引入jar包
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.27</version>
</dependency>
创建帮助类 FileUploadUtil
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.io.FileUtils;
public class FileUploadUtil {
public static String getFileSuffix(String fileName) {
return fileName.substring(fileName.lastIndexOf("."));
}
public static String newFileName(String suffix) {
String prefix = StringUtils.getUniqueNo();
return prefix + suffix;
}
public static void copyFile(String srcPath, String targetPath) throws Exception {
File srcFile = new File(srcPath);
File target = new File(targetPath);
if (!srcFile.exists()) {
throw new Exception("文件不存在!");
}
if (!srcFile.isFile()) {
throw new Exception("不是文件!");
}
if (!target.isDirectory()) {
throw new Exception("文件路径不存在!");
}
String fileName = srcPath.substring(srcPath.lastIndexOf("/") + 1);
File[] listFiles = target.listFiles();
for (File file : listFiles) {
if(fileName.equals(file.getName())){
fileName += "_1";
}
}
String newFileName = targetPath + File.separator + fileName;
File targetFile = new File(newFileName);
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(srcFile);
out = new FileOutputStream(targetFile);
byte[] buf = new byte[8 * 1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try{
if(in != null){
in.close();
}
}catch(Exception e){
System.out.println("关闭输入流错误!");
}
try{
if(out != null){
out.close();
}
}catch(Exception e){
System.out.println("关闭输出流错误!");
}
}
}
public static String getMD5Ssum(String path) {
StringBuffer sb = new StringBuffer("");
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(FileUtils.readFileToByteArray(new File(path)));
byte b[] = md.digest();
int d;
for (int i = 0; i < b.length; i++) {
d = b[i];
if (d < 0) {
d = b[i] & 0xff;
}
if (d < 16)
sb.append("0");
sb.append(Integer.toHexString(d));
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
Controller层
@SuppressWarnings("deprecation")
@RequestMapping(value = "/upload")
public String upload(@RequestParam("file")MultipartFile file) throws IOException {
byte[] imageData = file.getBytes();
String suffix = FileUploadUtil.getFileSuffix(file.getOriginalFilename());
String fileName = FileUploadUtil.newFileName(suffix);
System.out.println( saveImage(fileName,imageData,"/imgs/"));
return null;
}
public static String saveImage(String fileName, byte[] fileData, String relativePath) {
relativePath = relativePath == null ? "" : relativePath;
String filePath = relativePath + fileName;
return upload(filePath, fileData);
}
private static String upload(String filePath, byte[] fileData) {
String url =FAR_SERVICE_DIR+ filePath;
Client client = new Client();
WebResource resource = client.resource(url);
resource.put(String.class, fileData);
return filePath;
}
//指定一个临时路径作为上传目录
private static final String FAR_SERVICE_DIR = "http://xx.xx.xx/upload";//远程服务器接受文件的路由