```import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.util.UriUtils;
import java.io.File;
import java.util.Date;
/**
* @author Mr.Li
*/
public class DownloadUtil {
public static ResponseEntity<FileSystemResource> downloadFile(File file,String fileName) {
if (file == null) {
return null;
}
HttpHeaders headers = new HttpHeaders();
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" + UriUtils.encode(fileName, "UTF-8"));
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Last-Modified", new Date().toString());
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file));
}
}
@RequestMapping(value = "/xxx")
public ResponseEntity<FileSystemResource> downloadManual(@RequestParam(value = "fileName",required = false)String fileName) throws IOException{
String fileUrl = "xxx";
File file = new File(fileUrl);
fileName = fileName+"."+fileUrl.split("\\.")[1];
return DownloadUtil.downloadFile(file,fileName);
}
## 标题
上传图片工具类
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class FileTools {
/**
* 上传图片,并返回图片路径
*/
public static Map<String, Object> upload(MultipartFile file, HttpServletRequest request) {
Map<String, Object> map = new HashMap<>();
//获得当前操作系统的名称
String os = System.getProperty("os.name");
//过滤合法的文件类型
String name = file.getOriginalFilename(); //上传文件的真实名称
//获取后缀名
String expandedName = name.substring(name.lastIndexOf(".") + 1);
//获取真实路径
String path = "";
//创建新目录
/*String uri = File.separator + getNowDateStr(File.separator);
File dir = new File("/xxx/xxx/" + uri);*/
//在win系统下与linux以及mac下生成的路径
if (os.toLowerCase().startsWith("win")) {
path = System.getProperty("user.dir")+"\\xxx\\xx\\xx\\xxx\\xxx\\ixxx\\xxx\\";
} else { //linux 和mac
path = request.getServletContext().getRealPath("/");
path = File.separator+path.split("/")[1]+File.separator+"image"+File.separator;
}
//创建新文件
String fileName = getUniqueFileName()+ "."+expandedName;
File tempFile=new File(path,fileName);
try {
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
if (tempFile.exists()) {
tempFile.delete();
}
tempFile.createNewFile();
file.transferTo(tempFile);
} catch (IOException e) {
e.printStackTrace();
}
String path1 = tempFile.getPath();
map.put("url",path+fileName);
return map;
}
/**
* 获取当前日期字符串
*
* @param separator
* @return
*/
public static String getNowDateStr(String separator) {
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
//month 记得加一(因为默认从0开始计数)
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DATE);
return year + separator + month + separator + day;
}
//生成唯一的文件名
public static String getUniqueFileName() {
String str = UUID.randomUUID().toString();
return str.replace("-", "");
}
}
文件上传下载小工具类
最新推荐文章于 2024-04-27 00:30:00 发布