本文實例講述了Java實現的文件上傳下載工具類。分享給大家供大家參考,具體如下:
這是一個在Eclipse環境下采用Java語言實現文件上傳下載的工具類。和之前介紹的C#文件上傳下載工具類一樣,在上傳時,為避免文件名在服務器中重復,采用“服務器時間(定義到毫秒)+文件名+文件后綴“的方式作為服務器上的文件名;下載過程中利用 spring mvc ResponseEntity 做文件下載,返回的是字節流,下載成功后可自定義文件的保存路徑。
具體源碼如下所示:
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
|
package com.utils; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.io.FileUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; /** * 文件上傳下載工具類 * */ public class FileHelper { /** * 根據路徑確定目錄,沒有目錄,則創建目錄 * * @param path */ private static void createDir(String path) { File fileDir = new File(path); if (!fileDir.exists() && !fileDir.isDirectory()) { // 判斷/download目錄是否存在 fileDir.mkdir(); // 創建目錄 } } /** * 將文件名解析成文件的上傳路徑 * * @param fileName * @return 上傳到服務器的文件名 */ public static String transPath(String fileName, String path) { createDir(path); Date date = new Date(); SimpleDateFormat dateformat = new SimpleDateFormat( "yyyyMMddhhmmssSSS" ); // 定義到毫秒 String nowStr = dateformat.format(date); String filenameStr = fileName.substring( 0 , fileName.lastIndexOf( "." )); // 去掉后綴的文件名 String suffix = fileName.substring(fileName.lastIndexOf( "." ) + 1 ); // 后綴 if (fileName.trim() != "" ) { // 如果名稱不為"",說明該文件存在,否則說明該文件不存在 path += "\\" + filenameStr + nowStr + "." + suffix; // 定義上傳路徑 } return path; } /** * 提醒文件下載 * * @param fileName * @param path * @return */ public static ResponseEntity< byte []> downloadFile(String fileName, String path) { try { fileName = new String(fileName.getBytes( "GB2312" ), "ISO_8859_1" ); // 避免文件名中文不顯示 } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } File file = new File(path); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData( "attachment" , fileName); ResponseEntity< byte []> byteArr = null ; try { byteArr = new ResponseEntity< byte []>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK); } catch (IOException e) { e.printStackTrace(); } return byteArr; } /** * 將輸入流中的數據寫入字節數組 * * @param in * @return */ public static byte [] inputStream2ByteArray(InputStream in, boolean isClose) { byte [] byteArray = null ; try { int total = in.available(); byteArray = new byte [total]; in.read(byteArray); } catch (IOException e) { e.printStackTrace(); } finally { if (isClose) { try { in.close(); } catch (Exception e2) { System.out.println( "關閉流失敗" ); } } } return byteArray; } } |
希望本文所述對大家java程序設計有所幫助。
原文鏈接:http://blog.csdn.net/jianyuerensheng/article/details/78188621