日期:2014-05-17 浏览次数:20891 次
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import net.fckeditor.handlers.PropertiesLoader;
import net.fckeditor.jackie.util.FileUtil;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
@SuppressWarnings("serial")
@ParentPackage("json-default")
public class ImageUploadAction extends BaseAction
{
private File file;
private String fileFileName;
private String fileFileContentType;
private String message = "你已成功上传文件";
private Integer width = 0;
private Integer height = 0;
public File getFile()
{
return file;
}
public void setFile(File file)
{
this.file = file;
}
public String getFileFileName()
{
return fileFileName;
}
public void setFileFileName(String fileFileName)
{
this.fileFileName = fileFileName;
}
public String getFileFileContentType()
{
return fileFileContentType;
}
public void setFileFileContentType(String fileFileContentType)
{
this.fileFileContentType = fileFileContentType;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
public Integer getWidth()
{
return width;
}
public void setWidth(Integer width)
{
this.width = width;
}
public Integer getHeight()
{
return height;
}
public void setHeight(Integer height)
{
this.height = height;
}
// 跳转到添加广告页面
@Action(value = "toImageUpload", results = { @Result(name = "success", location = "/WEB-INF/banner/ImgFileUpload.jsp") })
public String toImageUpload()
{
return SUCCESS;
}
@Action(value = "imageUpload", results = { @Result(name = "success", type = "json") }, params = { "contentType", "text/html" })
public String imageUpload()
{
String path = ServletActionContext.getServletContext().getRealPath(PropertiesLoader.getUserFilesPath() + PropertiesLoader.getImageResourceTypePath()) + "/";
FileUtil.newFolder(path);
try
{
File f = this.getFile();
if (this.getFileFileName().endsWith(".exe"))
{
message = "对不起,你上传的文件格式不允许!!!";
return INPUT;
}
FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path + "/" + this.getFileFileName());
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1)
{
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
message = fileFileName;
File imageFile = new File(path + "/" + fileFileName);
Image image = ImageIO.read(imageFile);
width = image.getWidth(null);
height = image.getHeight(null);
}
catch (Exception e)
{
e.printStackTrace();
message = "对不起,文件上传失败了!!!!";
}
return SUCCESS;
}
}