This commit is contained in:
罗祥 2018-12-20 10:57:37 +08:00
parent 47d65ac6b3
commit af975f5eb1
4 changed files with 83 additions and 29 deletions

View File

@ -744,41 +744,53 @@ public class FileController {
package com.heibaiying.utils; package com.heibaiying.utils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.*; import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/** /**
* @author : heibaiying * @author : heibaiying
* @description : 文件上传工具类 * @description : 文件上传工具类
*/ */
public class FileUtil { public class FileUtil {
public static String saveFile(MultipartFile file, String path) { public static String saveFile(MultipartFile file, String path) {
InputStream inputStream = null;
FileOutputStream outputStream = null;
String fullPath = path + File.separator + file.getOriginalFilename(); String fullPath = path + File.separator + file.getOriginalFilename();
try { try {
File saveDir = new File(path); File saveDir = new File(path);
if (!saveDir.exists()) { if (!saveDir.exists()) {
saveDir.mkdirs(); saveDir.mkdirs();
} }
FileOutputStream outputStream = new FileOutputStream(new File(fullPath)); outputStream = new FileOutputStream(new File(fullPath));
InputStream inputStream = file.getInputStream(); inputStream = file.getInputStream();
byte[] bytes = new byte[1024 * 1024]; byte[] bytes = new byte[1024 * 1024];
int read; int read;
while ((read = inputStream.read(bytes)) != -1) { while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read); outputStream.write(bytes, 0, read);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
return fullPath; return fullPath;
} }
} }
``` ```
4.新建用于上传的jsp页面上传文件时表单必须声明 enctype="multipart/form-data" 4.新建用于上传的jsp页面上传文件时表单必须声明 enctype="multipart/form-data"

View File

@ -2,34 +2,49 @@ package com.heibaiying.utils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/** /**
* @author : heibaiying * @author : heibaiying
* @description : 文件上传工具类 * @description : 文件上传工具类
*/ */
public class FileUtil { public class FileUtil {
public static String saveFile(MultipartFile file, String path) { public static String saveFile(MultipartFile file, String path) {
InputStream inputStream = null;
FileOutputStream outputStream = null;
String fullPath = path + File.separator + file.getOriginalFilename(); String fullPath = path + File.separator + file.getOriginalFilename();
try { try {
File saveDir = new File(path); File saveDir = new File(path);
if (!saveDir.exists()) { if (!saveDir.exists()) {
saveDir.mkdirs(); saveDir.mkdirs();
} }
FileOutputStream outputStream = new FileOutputStream(new File(fullPath)); outputStream = new FileOutputStream(new File(fullPath));
InputStream inputStream = file.getInputStream(); inputStream = file.getInputStream();
byte[] bytes = new byte[1024 * 1024]; byte[] bytes = new byte[1024 * 1024];
int read; int read;
while ((read = inputStream.read(bytes)) != -1) { while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read); outputStream.write(bytes, 0, read);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
return fullPath; return fullPath;
} }

View File

@ -735,41 +735,53 @@ public class FileController {
package com.heibaiying.utils; package com.heibaiying.utils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.*; import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/** /**
* @author : heibaiying * @author : heibaiying
* @description : 文件上传工具类 * @description : 文件上传工具类
*/ */
public class FileUtil { public class FileUtil {
public static String saveFile(MultipartFile file, String path) { public static String saveFile(MultipartFile file, String path) {
InputStream inputStream = null;
FileOutputStream outputStream = null;
String fullPath = path + File.separator + file.getOriginalFilename(); String fullPath = path + File.separator + file.getOriginalFilename();
try { try {
File saveDir = new File(path); File saveDir = new File(path);
if (!saveDir.exists()) { if (!saveDir.exists()) {
saveDir.mkdirs(); saveDir.mkdirs();
} }
FileOutputStream outputStream = new FileOutputStream(new File(fullPath)); outputStream = new FileOutputStream(new File(fullPath));
InputStream inputStream = file.getInputStream(); inputStream = file.getInputStream();
byte[] bytes = new byte[1024 * 1024]; byte[] bytes = new byte[1024 * 1024];
int read; int read;
while ((read = inputStream.read(bytes)) != -1) { while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read); outputStream.write(bytes, 0, read);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
return fullPath; return fullPath;
} }
} }
``` ```
4.新建用于上传的jsp页面上传文件时表单必须声明 enctype="multipart/form-data" 4.新建用于上传的jsp页面上传文件时表单必须声明 enctype="multipart/form-data"

View File

@ -3,33 +3,48 @@ package com.heibaiying.utils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.*; import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/** /**
* @author : heibaiying * @author : heibaiying
* @description : 文件上传工具类 * @description : 文件上传工具类
*/ */
public class FileUtil { public class FileUtil {
public static String saveFile(MultipartFile file, String path) { public static String saveFile(MultipartFile file, String path) {
InputStream inputStream = null;
FileOutputStream outputStream = null;
String fullPath = path + File.separator + file.getOriginalFilename(); String fullPath = path + File.separator + file.getOriginalFilename();
try { try {
File saveDir = new File(path); File saveDir = new File(path);
if (!saveDir.exists()) { if (!saveDir.exists()) {
saveDir.mkdirs(); saveDir.mkdirs();
} }
FileOutputStream outputStream = new FileOutputStream(new File(fullPath)); outputStream = new FileOutputStream(new File(fullPath));
InputStream inputStream = file.getInputStream(); inputStream = file.getInputStream();
byte[] bytes = new byte[1024 * 1024]; byte[] bytes = new byte[1024 * 1024];
int read; int read;
while ((read = inputStream.read(bytes)) != -1) { while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read); outputStream.write(bytes, 0, read);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
return fullPath; return fullPath;
} }