web图片上传


1、写一个上传的jsp页面upload_test.jsp,内容如下:
<body>
<center>
<font color="red"><s:fielderror/></font>
<s:form action="upload" method="post" enctype="multipart/form-data">
<s:file name="file" label="文件1"></s:file>
<s:file name="file" label="文件2"></s:file>
<s:submit label="上传"/>
</s:form>
</center>
</body>

解析:A、 form里面的method必须是post,enctype="multipart/form-data"上传文件必须这样写
 B、<s:fielderror/>这个是图片格式或者大小出错的错误提示---要在struts.xml里面先配置
 C、name="file",批量上传name的值要一样
 
 
 
2、创建action:
@Component("upload")
@Scope("prototype")
public class UploadAction extends BaseAction  {
    //上传文件存放路径   
    private final static String UPLOADDIR = "/upload";   
    //上传文件集合   
    private List<File> file;   
    //上传文件名集合   
    private List<String> fileFileName;   
    //上传文件内容类型集合   
    private List<String> fileContentType;   
    public List<File> getFile() {   
        return file;   
    }   

    public void setFile(List<File> file) {   
        this.file = file;   
    }   

   public List<String> getFileFileName() {   
       return fileFileName;   
   }   

    public void setFileFileName(List<String> fileFileName) {   
        this.fileFileName = fileFileName;   
    }   

    public List<String> getFileContentType() {   
        return fileContentType;   
    }   

    public void setFileContentType(List<String> fileContentType) {   
        this.fileContentType = fileContentType;   
    }   

    public String execute() throws Exception {   
        for (int i = 0; i < file.size(); i++) {   
            //循环上传每个文件   
            uploadFile(i);   
        }   
        return "success";   
    }   

    //执行上传功能   
    private void uploadFile(int i) throws FileNotFoundException, IOException {   
        try {   
            InputStream in = new FileInputStream(file.get(i));   
            String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR);  
            File fileLocation = new File(dir);  
            //此处也可以在应用根目录手动建立目标上传目录  
            if(!fileLocation.exists()){  
                boolean isCreated  = fileLocation.mkdir();  
                if(!isCreated) {  
                    //目标上传目录创建失败,可做其他处理,例如抛出自定义异常等,一般应该不会出现这种情况。  
                    return;  
                }  
            }  
            String fileName=this.getFileFileName().get(i);  
            File uploadFile = new File(dir, fileName);   
            OutputStream out = new FileOutputStream(uploadFile);   
            byte[] buffer = new byte[1024 * 1024];   
            int length;   
            while ((length = in.read(buffer)) > 0) {   
                out.write(buffer, 0, length);   
            }   
            in.close();   
            out.close();   
        } catch (FileNotFoundException ex) {   
            System.out.println("上传失败!");  
            ex.printStackTrace();   
        } catch (IOException ex) {   
            System.out.println("上传失败!");  
            ex.printStackTrace();   
        }   
    }   
}
说明:
a、 file 为上传文件集合,里面包含了你上传的所有文件;
b、 fileFileName 包含了你上传文件名的集合(无需在JSP额外写出这项,他会自动将名字存入这个变量中)
c、  fileContentType 通过此变量可以查看到各个文件的类型   
   


3、配置struts.xml文件
<!-- 配置action -->
<package name="default" extends="struts-default">
    <action name="upload" class="upload">
        <!-- 限制图片的格式和图片的大小 -->
        <interceptor-ref name="fileUpload">
            <param name="allowedTypes">
                image/bmp,image/png,image/gif,image/jpeg,image/jpg
            </param>
            <param name="maximumSize">102400</param>
        </interceptor-ref>

        <!-- 默认的拦截器,必须要写 -->
        <interceptor-ref name="defaultStack" />
        <result name="success">/show_img.jsp</result>
    </action>
</package>

4、把上传的图片显示出来 ,文件为show_img.jsp
<body>
 <s:iterator value="fileFileName" status="length">
    <img src='upload/<s:property value="fileFileName.get(#length.index)"/>'>
 </s:iterator>
</body>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值