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

View File

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

View File

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

View File

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