SSH框架網上商城項目第13戰之Struts2文件上傳功效。本站提示廣大學習愛好者:(SSH框架網上商城項目第13戰之Struts2文件上傳功效)文章只能為提供參考,不一定能成為您想要的結果。以下是SSH框架網上商城項目第13戰之Struts2文件上傳功效正文
上一節我們做完了添加和更新商品的功效,這兩個部門裡有觸及到商品圖片的上傳,並沒有具體講解。為此,這篇文章具體引見一下Struts2完成文件上傳的功效。
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辦法可以不消和屬性名一樣,然則前台傳出去時的參數得和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. 完成文件上傳對象類
//文件上傳對象類接口
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中完成文件上傳的功效了:
@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中的代碼量就很少了,這也是封裝帶來的長處。
@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
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。