import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class ImageUploadForm extends ActionForm{
private FormFile imagePath; //图片路径
private String pathId; //前台<input>图片路径元素的ID
private String imgId; //前台<img> 图片显示元素的ID
/**
* @return Returns the imagePath.
*/
public FormFile getImagePath() {
return imagePath;
}
/**
* @param imagePath The imagePath to set.
*/
public void setImagePath(FormFile imagePath) {
this.imagePath = imagePath;
}
/**
* @return Returns the imgId.
*/
public String getImgId() {
return imgId;
}
/**
* @param imgId The imgId to set.
*/
public void setImgId(String imgId) {
this.imgId = imgId;
}
/**
* @return Returns the pathId.
*/
public String getPathId() {
return pathId;
}
/**
* @param pathId The pathId to set.
*/
public void setPathId(String pathId) {
this.pathId = pathId;
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class ImageUploadAction extends Action {
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse)
{
ImageUploadForm imageUploadForm = (ImageUploadForm)actionForm;
ImageUpload imageUpload = new ImageUpload();
String pathId=(String)imageUploadForm.getPathId(); //前台<input>元素的ID
String imgId=(String)imageUploadForm.getImgId();//前台<img> 图片显示元素的ID
FormFile theFile=imageUploadForm.getImagePath();//图片路径
String fileName="none.jpg";
if(theFile!=null&&!theFile.equals("")){
String path=this.getServlet().getServletContext().getRealPath("upload//congress//picture");
fileName=imageUpload.getUName(theFile);
try {
//上传文件
imageUpload.upload(theFile,path,fileName);
} catch (FileNotFoundException e) {
httpServletRequest.setAttribute("exception","文件未找到!"+e);
return actionMapping.findForward("exception");
} catch (IOException e) {
httpServletRequest.setAttribute("exception","文件上传异常!"+e);
return actionMapping.findForward("exception");
}
}
httpServletRequest.setAttribute("pathId",pathId);
httpServletRequest.setAttribute("pathId",pathId);
httpServletRequest.setAttribute("pic",fileName);
return actionMapping.findForward("success");
}
}
import org.apache.struts.upload.FormFile;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ImageUpload {
/**
* 图片上传
* @param formFile
* @param path
* @param uName
* @throws FileNotFoundException
* @throws IOException
*/
public void upload(FormFile formFile,String path,String uName) throws FileNotFoundException, IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = formFile.getInputStream();
OutputStream bos = new FileOutputStream(path+"//"+uName);
int bytesRead = 0;
byte buffer[] = new byte[8192];
while((bytesRead = stream.read(buffer, 0, 8192)) != -1)
bos.write(buffer, 0, bytesRead);
bos.close();
}
/**
* 获取文件上传后名称
* @param formFile
* @return
*/
public String getUName(FormFile formFile){
return this.uid()+this.postfix(formFile);
}
/**
* 文件数据库编号(别名)
* @return
*/
private String uid()
{
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
Date d = new Date();
return format.format(d);
}
/**
* 文件后缀
* @param fileName
* @return
*/
public String postfix(FormFile formFile){
if (formFile != null && !formFile.getFileName().equals("")) {
String name = formFile.getFileName();
return name.substring(name.indexOf("."), name.length());
} else {
return null;
}
}
}