再问struts多文件上传的问题

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;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值