根据路径下载到指定文件下
/**
* 根据路径下载到指定文件下
* @param urlList
* @param path
* @return
*/
public static boolean downloadPic(String urlList,String path) {
URL url = null;
try {
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
return true;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
判断路径是否已存在 并 新建文件夹
File targetFile = new File("F:/Java/MySQL/image_test2/");
if (!targetFile.exists()) {
targetFile.mkdirs();
}
File转换成MultipartFile
- 导入jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-mock -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
2.代码
public static void main(String[] args) throws Exception {
String strUrl = "F:\\图片.jpg";
File file = new File(strUrl);
InputStream inputStream = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile(file.getName(), inputStream);
log.info("file转multipartFile成功",multipartFile);
}
MultipartFile转file
File zipFile = null;
if(fil.equals("")||fil.getSize()<=0){
fil = null;
}else {
InputStream ins = null;
ins = fil.getInputStream();
zipFile = new File(fil.getOriginalFilename());
inputStreamToFile(ins, zipFile);
ins.close();
}
public static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
将目标文件压缩为zip文件下载到桌面
@RequestMapping("/download")
@ResponseBody
public void download(HttpServletRequest request, HttpServletResponse response) throws Exception{
response.setCharacterEncoding("UTF-8");
//获得要下载的文件名
String fileName = "导入用户示例.xlsx";
String fileName2 = "张1.jpg";
String fileSaveRootPath = "D:\\Work\\fw\\profile\\avatar\\示例\\";
//得到要下载的文件
File file = new File(fileSaveRootPath, fileName);
File file2 = new File(fileSaveRootPath, fileName2);
System.out.println("Excel文件保存路径1:" + fileSaveRootPath + fileName);
System.out.println("Excel文件保存路径2:" + fileSaveRootPath + fileName2);
//如果文件不存在
if (!file.exists() ) {
request.setAttribute("message", "您要下载的资源已被删除!!");
request.getRequestDispatcher("/message.jsp").forward(request, response);
return ;
}
//先压缩
String zipName = "导入用户示例.zip";
//获取当前用户桌面
File desktopDir = FileSystemView.getFileSystemView() .getHomeDirectory();
String desktopPath = desktopDir.getAbsolutePath();
String zipPath = desktopPath+"\\" + zipName;
ZipOutputStream zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipPath)));
ZipEntry zEntry = new ZipEntry(file.getName());
zipOutput.putNextEntry(zEntry);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
int read = 0;
while((read = bis.read(buffer)) != -1){
zipOutput.write(buffer, 0, read);
}
zEntry = new ZipEntry(file2.getName());
zipOutput.putNextEntry(zEntry);
bis = new BufferedInputStream(new FileInputStream(file2));
while((read = bis.read(buffer)) != -1){
zipOutput.write(buffer, 0, read);
}
bis.close();
zipOutput.close();
//创建输出流,下载zip
try(OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(new File(zipPath));){
//设置响应头,控制浏览器下载该文件
response.setHeader("Content-Type","application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename="+java.net.URLEncoder.encode(zipName, "UTF-8"));
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
System.out.println("zip下载路径:"+zipPath);
}finally {
try {
/*//删除压缩包
File zfile = new File(zipPath);
zfile.delete();*/
}catch (Exception e){
e.printStackTrace();
}
}
}