- 任何非压缩格式下载
package com.pisx.pd.eco.util;
import java.io.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import com.pisx.pd.commom.utils.FileSizeUnitTransform;
import com.pisx.pd.eco.config.FilePathConfig;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class FileUtils {
public static String downloadFile(HttpServletResponse response, String fileName, String filePath) {
InputStream inStream = null;
FileInputStream fis = null;
ServletOutputStream servletOs = null;
try {
File file = new File(filePath, fileName);
if (file.exists()) {
response.reset();
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
int fileLength = (int)file.length();
response.setContentLength(fileLength);
if (fileLength != 0) {
fis = new FileInputStream(file);
inStream = new BufferedInputStream(fis);
byte[] buf = new byte[4096];
servletOs = response.getOutputStream();
int readLength;
while (((readLength = inStream.read(buf)) != -1)) {
servletOs.write(buf, 0, readLength);
}
}
return "下载成功";
} else {
return "文件不存在";
}
} catch (Exception e) {
e.printStackTrace();
return "下载文件出错";
} finally {
if (inStream != null) {
try {
fis.close();
inStream.close();
} catch (IOException e) {
log.info(e.getMessage());
}
}
if (servletOs != null) {
try {
servletOs.flush();
servletOs.close();
} catch (IOException e) {
log.info(e.getMessage());
}
}
}
}
public static String downloadFile(HttpServletResponse response, InputStream inputStream, String fileName) {
ServletOutputStream servletOs = null;
try {
response.reset();
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
byte[] buf = new byte[4096];
servletOs = response.getOutputStream();
int readLength;
while (((readLength = inputStream.read(buf)) != -1)) {
servletOs.write(buf, 0, readLength);
}
return "下载成功";
} catch (Exception e) {
e.printStackTrace();
return "下载文件出错";
} finally {
if (servletOs != null) {
try {
servletOs.flush();
servletOs.close();
} catch (IOException e) {
log.info(e.getMessage());
}
}
}
}
public static Map<String, String> uploadFile(MultipartFile file, String filePath, String fileName) {
String fileUrl = filePath + fileName;
String fileSize = FileSizeUnitTransform.GetFileSize(file.getSize());
int index = fileName.lastIndexOf(".");
String fileFormat = fileName.substring(index + 1);
File filed = new File(FilePathConfig.PATH + fileUrl);
if (!filed.getParentFile().exists()) {
boolean mkdirs = filed.getParentFile().mkdirs();
if (Boolean.FALSE.equals(mkdirs)) {
return Collections.emptyMap();
}
}
try {
file.transferTo(filed);
} catch (Exception ex) {
log.error(ex.getMessage());
}
Map<String, String> map = new HashMap<>(3);
map.put("fileUrl", fileUrl);
map.put("fileSize", fileSize);
map.put("fileFormat", fileFormat);
return map;
}
private FileUtils() {
throw new IllegalStateException("Utility class");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 压缩包格式下载
package com.pisx.pd.eco.util;
import com.pisx.pd.commom.utils.FileSizeUnitTransform;
import com.pisx.pd.datasource.lib.entity.eco.CarbonMore;
import com.pisx.pd.datasource.lib.entity.eco.MineFileDto;
import com.pisx.pd.datasource.lib.entity.eco.StatementTemplate;
import com.pisx.pd.eco.config.FilePathConfig;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class UploadFileUtil {
static class FileContent {
String fileUrl = null;
String fileName = null;
String filePath = null;
String fileSize = null;
String fileFormat = null;
public String getFileUrl() {
return fileUrl;
}
public void setFileUrl(String fileUrl) {
this.fileUrl = fileUrl;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileSize() {
return fileSize;
}
public void setFileSize(String fileSize) {
this.fileSize = fileSize;
}
public String getFileFormat() {
return fileFormat;
}
public void setFileFormat(String fileFormat) {
this.fileFormat = fileFormat;
}
public void setExceptFileFormatName(String substring) {}
}
public static List<FileContent> uploadFileUtil(MultipartFile[] files, String fileUrl) {
if (files != null && files.length > 0) {
try {
List<FileContent> list = new ArrayList<>();
for (MultipartFile item : files) {
FileContent f = new FileContent();
f.setFileName(item.getOriginalFilename());
f.setFileSize(FileSizeUnitTransform.GetFileSize(item.getSize()));
int index = item.getOriginalFilename().lastIndexOf(".");
f.setExceptFileFormatName(item.getOriginalFilename().substring(0, index));
f.setFileName(item.getOriginalFilename().substring(index + 1));
f.setFileUrl(fileUrl);
f.setFilePath(fileUrl + item.getOriginalFilename());
list.add(f);
File file = new File(fileUrl + item.getOriginalFilename());
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
public static byte[] getPackage(MineFileDto file) {
byte[] bag = null;
try {
String filePath = file.getFile_path();
bag = getBytesByFile(filePath);
} catch (Exception e) {
e.printStackTrace();
}
return bag;
}
public static byte[] getPackageCarbonMore(CarbonMore file) {
byte[] bag = null;
try {
String filePath = file.getFile_path();
bag = getBytesByFile(filePath);
} catch (Exception e) {
e.printStackTrace();
}
return bag;
}
public static byte[] getPackages(String filePath) {
byte[] bag = null;
try {
bag = getBytesByFile(filePath);
} catch (Exception e) {
e.printStackTrace();
}
return bag;
}
@Nullable
private static byte[] getBytesByFile(String filePath) {
try {
File file = new File(FilePathConfig.PATH + filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
byte[] data = bos.toByteArray();
bos.close();
return data;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void downloadBatchByFile(HttpServletResponse response, Map<String, byte[]> files, String zipName) {
try {
response.setContentType("application/x-msdownload");
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(zipName, "UTF-8"));
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
BufferedOutputStream bos = new BufferedOutputStream(zos);
for (Map.Entry<String, byte[]> entry : files.entrySet()) {
String fileName = entry.getKey();
byte[] file = entry.getValue();
BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(file));
zos.putNextEntry(new ZipEntry(fileName));
int len = 0;
byte[] buf = new byte[10 * 1024];
while ((len = bis.read(buf, 0, buf.length)) != -1) {
bos.write(buf, 0, len);
}
bis.close();
bos.flush();
}
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194