@Override
public String uploadFile(MultipartFile file, HttpServletRequest request) throws FileException {
FileUtils.deleteFile(file);
String uploadfile = FileUtils.uploadfile(file);
StringBuffer url = request.getRequestURL();
String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getSession().getServletContext().getContextPath()).toString();
return tempContextUrl + "/upgrued/download" + uploadfile;
}
@Override
public void downloadFile(String filePath, HttpServletResponse response) throws UnsupportedEncodingException {
String path = System.getProperty("user.dir");
File file = new File(path + filePath);
String filename = file.getName();
response.reset();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "utf8"));
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setContentType("application/octet-stream;charset=UTF-8");
try (
FileInputStream in = new FileInputStream(file);
) {
OutputStream out = response.getOutputStream();
int len;
byte[] bytes = new byte[1024];
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
out.close();
} catch (IOException ex) {
logger.info("文件下载异常", ex);
}
}
@Override
public void downloadRdpFile(TbEquipmentDTO dto, HttpServletResponse response){
String ipAddress = dto.getIpAddress();
String remoteLoginUser = dto.getRemoteLoginUser();
String outRdpFileSrting = "auto connect:i:1\r\n"
.concat("full address:s:").concat(ipAddress).concat("\r\n")
.concat("username:s:").concat(remoteLoginUser).concat("\r\n");
try (
OutputStream out = response.getOutputStream();
ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(outRdpFileSrting.getBytes(StandardCharsets.UTF_8));
) {
String filename = "device-".concat(ipAddress).concat(".rdp");
response.reset();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "utf8"));
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setContentType("application/octet-stream;charset=UTF-8");
int len;
byte[] bytes = new byte[1024];
while ((len = tInputStringStream.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
} catch (UnsupportedEncodingException ex) {
logger.info("类型转换异常", ex);
} catch (IOException ex) {
logger.info("文件下载异常", ex);
}
}
工具类
package com.yyt.common.core.utils.file;
import cn.hutool.core.io.FileUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.UUID;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class FileUtils extends FileUtil {
public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException {
String percentEncodedFileName = percentEncode(realFileName);
StringBuilder contentDispositionValue = new StringBuilder();
contentDispositionValue.append("attachment; filename=")
.append(percentEncodedFileName)
.append(";")
.append("filename*=")
.append("utf-8''")
.append(percentEncodedFileName);
response.addHeader("Access-Control-Allow-Origin" , "*");
response.addHeader("Access-Control-Expose-Headers" , "Content-Disposition,download-filename");
response.setHeader("Content-disposition" , contentDispositionValue.toString());
response.setHeader("download-filename" , percentEncodedFileName);
}
public static String percentEncode(String s) throws UnsupportedEncodingException {
String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
return encode.replaceAll("\\+" , "%20");
}
public static String uploadfile(MultipartFile file) {
String path = System.getProperty("user.dir") + "/upload/";
String originalFilename = file.getOriginalFilename();
int lastIndexOf = originalFilename.lastIndexOf(".");
String suffix = originalFilename.substring(lastIndexOf);
File realPath = new File(path);
if (!realPath.exists()) {
realPath.mkdirs();
}
String name = file.getOriginalFilename();
try {
file.transferTo(new File(realPath +"/"+ name));
} catch (IOException e) {
e.printStackTrace();
}
return "/upload/" + name;
}
public static void deleteFile(MultipartFile file){
String path = System.getProperty("user.dir");
path = path + file.getOriginalFilename();
File fileDel = new File(path);
boolean isOk = fileDel.exists();
if(isOk){
fileDel.delete();
}
}
}