1.首先有一个包装类
package com.marvin.book.pojo;
import java.io.File;
public class BookFile {
private File image;
private String imageFileName;
private String imageContentType;
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
}
前台上传图片的name是这个类的一个属性 name = 后台变量名.image
下面是前台的举例(extjs)
items:[{
xtype:'filefield',
name:'photos.image',
labelWidth:60,
msgTarget: 'side',
anchor: '100%',
buttonText: '选择照片',
fieldLabel : '上传图片'
}
java前台更简单哟,直接加上一个file将name命名为 name = 后台变量名.image 就好了
3.后台承接到对象就直接可以存储了,下面是获取存储位置的文件路径的方法
servletContext.getRealPath("upload");
String savePath = realPath + "/" + uploadFileName
package com.marvin.book.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import org.junit.Test;
import com.marvin.book.pojo.BookFile;
public class FileUploadUtil {
private String filePath = null;
public void setFilePath(String filePath) {
this.filePath = filePath;
}
//一个方法一个功能
public String getExt(String fileName){
return fileName.substring(fileName.lastIndexOf(".")+1);
}
public String createFileName(String fileName){
return UUID.randomUUID().toString()+"."+getExt(fileName);
}
//删除文件
public void delete(String fileName){
new File(fileName).delete();
}
//进行另一个文件位置的存储
public String uploadFile(File file, String fileName, String path){
if(path != null){
filePath = path;
}
return this.uploadFile(file, fileName);
}
public String[] bankImage(String path){
File file = new File(path);
return file.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith("gif");
}
});
}
//上传文件返回新的文件名
public String uploadFile(File file, String fileName){
//生成新的文件名
String newName = createFileName(fileName);
//文件上传
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(file);
output = new FileOutputStream(filePath + "/"+newName);
byte[] b =new byte[1024];
int length = 0;
while((length=input.read(b)) != -1){
output.write(b, 0, length);
}
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
try {
input.close();
} catch (Exception e) {
throw new RuntimeException(e);
} finally{
try {
output.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return newName;
}
}
小注:获取图片实际路径的方法有很多,自己可以去网上进行搜索,比如: ServletContextAware(Action内部即可直接使用ServletContext对象)
还有需要注意:
Struts2配置文件中的struts.multipart.saveDir起什么作用?
原来初步感觉这个文件夹就服务端保存上传文件的文件夹,不过根本不是这么回事!
这个文件夹只是用来保存上传文件的“临时路径”,文件上传完毕后就会从此路径移除。
如果不配置将使用默认的 javax.servlet.context.tempdir 来保存临时文件。
所以这个目录设不设无所谓,尼玛又误导我一次!!!