上一節我們做完了添加和更新商品的功能,這兩個部分里有涉及到商品圖片的上傳,并沒有詳細解說。為此,這篇文章詳細介紹一下Struts2實現文件上傳的功能。
1. 封裝文件信息
我們首先得有一個Model來封裝文件的信息,這個Model里需要有三個屬性:文件、文件類型和文件名。針對我們要傳的圖片,我們新建一個Model如下:
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
|
public class FileImage { private File file; private String contentType; private String filename; public File getFile() { return file; } public String getContentType() { return contentType; } public String getFilename() { return filename; } public void setUpload(File file) { //set方法可以不用和屬性名一樣,但是前臺傳進來時的參數得和set方法名相同。即前臺傳的參數為fileImage.upload this .file = file; } public void setUploadContentType(String contentType) { this .contentType = contentType; } public void setUploadFileName(String filename) { this .filename = filename; } } |
這樣Model就寫好了,考慮到文件上傳的邏輯不是單個Action所特有的,所以我們將文件上傳的邏輯寫到工具類中,這樣可供所有的Action調用。所以我們新建一個文件上傳工具類(為了面向接口編程,我們也將工具類抽出個接口):
2. 完成文件上傳工具類
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
|
//文件上傳工具類接口 public interface FileUpload { //實現文件上傳的功能,返回上傳后新的文件名稱 public abstract String uploadFile(FileImage fileImage); } //文件上傳工具類具體實現 @Component ( "fileUpload" ) public class FileUploadUtil implements FileUpload { private String filePath; @Value ( "#{prop.filePath}" ) //@Value表示去beans.xml文件中找id="prop"的bean,它是通過注解的方式讀取properties配置文件的,然后去相應的配置文件中讀取key=filePath的值 public void setFilePath(String filePath) { System.out.println(filePath); this .filePath = filePath; } //1. 通過文件名獲取擴展名 private String getFileExt(String fileName) { return FilenameUtils.getExtension(fileName); } //2. 生成UUID隨機數,作為新的文件名 private String newFileName(String fileName) { String ext = getFileExt(fileName); return UUID.randomUUID().toString() + "." + ext; } //實現文件上傳的功能,返回上傳后新的文件名稱 @Override public String uploadFile(FileImage fileImage) { //獲取新唯一文件名 String pic = newFileName(fileImage.getFilename()); try { FileUtil.copyFile(fileImage.getFile(), new File(filePath, pic)); //第一個參數是上傳的文件,第二個參數是將文件拷貝到新路徑下 return pic; } catch (Exception e) { throw new RuntimeException(e); } finally { fileImage.getFile().delete(); } } } |
上面有個@Value注解,是從properties文件中獲取文件要存入的路徑的,具體可參見:Spring獲取配置文件信息 。
3. 在Action中注入封裝文件類和工具類
寫好了文件封裝類和上傳文件工具類后,我們需要將這兩個對象注入到我們的Action中,這樣就可以在Action中實現文件上傳的功能了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@Controller ( "baseAction" ) @Scope ( "prototype" ) public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T> { //封裝了圖片信息的類 protected FileImage fileImage; //上傳文件工具類 @Resource protected FileUpload fileUpload; public FileImage getFileImage() { return fileImage; } public void setFileImage(FileImage fileImage) { this .fileImage = fileImage; } //省略其他無關代碼…… } |
4. 實現文件的上傳
好了,現在我們可以在ProductAction中去實現文件上傳了,工具類寫好的話,在Action中的代碼量就很少了,這也是封裝帶來的優點。
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
|
@Controller ( "productAction" ) @Scope ( "prototype" ) public class ProductAction extends BaseAction<Product> { //省略其他無關代碼…… public void save() throws Exception { //fileUpload工具類被抽取了,uploadFile方法直接接受一個fileImage對象,返回新的圖片名 String pic = fileUpload.uploadFile(fileImage); model.setPic(pic); model.setDate( new Date()); System.out.println(model); //商品信息入庫 productService.save(model); } public void update() { String pic = fileUpload.uploadFile(fileImage); model.setPic(pic); model.setDate( new Date()); System.out.println(model); //更新商品 productService.update(model); } } |
這樣我們就完成了從前臺上傳文件的功能。
原文地址:http://blog.csdn.net/eson_15/article/details/51366384
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。