SSH框架網(wǎng)上商城項目第13戰(zhàn)之Struts2文件上傳功能
上一節(jié)我們做完了添加和更新商品的功能,這兩個部分里有涉及到商品圖片的上傳,并沒有詳細解說。為此,這篇文章詳細介紹一下Struts2實現(xiàn)文件上傳的功能。
1. 封裝文件信息
我們首先得有一個Model來封裝文件的信息,這個Model里需要有三個屬性:文件、文件類型和文件名。針對我們要傳的圖片,我們新建一個Model如下:
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方法可以不用和屬性名一樣,但是前臺傳進來時的參數(shù)得和set方法名相同。即前臺傳的參數(shù)為fileImage.upload
this.file = file;
}
public void setUploadContentType(String contentType) {
this.contentType = contentType;
}
public void setUploadFileName(String filename) {
this.filename = filename;
}
}
這樣Model就寫好了,考慮到文件上傳的邏輯不是單個Action所特有的,所以我們將文件上傳的邏輯寫到工具類中,這樣可供所有的Action調(diào)用。所以我們新建一個文件上傳工具類(為了面向接口編程,我們也將工具類抽出個接口):
2. 完成文件上傳工具類
//文件上傳工具類接口
public interface FileUpload {
//實現(xiàn)文件上傳的功能,返回上傳后新的文件名稱
public abstract String uploadFile(FileImage fileImage);
}
//文件上傳工具類具體實現(xiàn)
@Component("fileUpload")
public class FileUploadUtil implements FileUpload {
private String filePath;
@Value("#{prop.filePath}")
//@Value表示去beans.xml文件中找id="prop"的bean,它是通過注解的方式讀取properties配置文件的,然后去相應(yīng)的配置文件中讀取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隨機數(shù),作為新的文件名
private String newFileName(String fileName) {
String ext = getFileExt(fileName);
return UUID.randomUUID().toString() + "." + ext;
}
//實現(xiàn)文件上傳的功能,返回上傳后新的文件名稱
@Override
public String uploadFile(FileImage fileImage) {
//獲取新唯一文件名
String pic = newFileName(fileImage.getFilename());
try {
FileUtil.copyFile(fileImage.getFile(), new File(filePath, pic));//第一個參數(shù)是上傳的文件,第二個參數(shù)是將文件拷貝到新路徑下
return pic;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
fileImage.getFile().delete();
}
}
}
上面有個@Value注解,是從properties文件中獲取文件要存入的路徑的,具體可參見:Spring獲取配置文件信息 。
3. 在Action中注入封裝文件類和工具類
寫好了文件封裝類和上傳文件工具類后,我們需要將這兩個對象注入到我們的Action中,這樣就可以在Action中實現(xiàn)文件上傳的功能了:
@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;
}
//省略其他無關(guān)代碼……
}
4. 實現(xiàn)文件的上傳
好了,現(xiàn)在我們可以在ProductAction中去實現(xiàn)文件上傳了,工具類寫好的話,在Action中的代碼量就很少了,這也是封裝帶來的優(yōu)點。
@Controller("productAction")
@Scope("prototype")
public class ProductAction extends BaseAction<Product> {
//省略其他無關(guān)代碼……
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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Spring Cloud Zuul中路由配置細節(jié)
本篇文章主要介紹了詳解Spring Cloud Zuul中路由配置細節(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
springboot+idea+maven 多模塊項目搭建的詳細過程(連接數(shù)據(jù)庫進行測試)
這篇文章主要介紹了springboot+idea+maven 多模塊項目搭建的詳細過程(連接數(shù)據(jù)庫進行測試),本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
Spring中@PropertySource和@Value注解詳解
這篇文章主要介紹了Spring中@PropertySource和@Value注解詳解,@PropertySource注解可以方便和靈活的向Spring的環(huán)境容器(org.springframework.core.env.Environment Environment)中注入一些屬性,這些屬性可以在Bean中使用,需要的朋友可以參考下2023-11-11

