注册 登录
编程论坛 J2EE论坛

如何实现表单提交和上传图片同时完成

海风308 发布于 2008-11-07 14:22, 1909 次点击
如题,我开始想完成该动作,在一个form中,要上传图片,需要加上form 表单的属性:enctype="multipart/form-data",但是到action页面就得不到其他的文本参数的值。
要是去掉enctype="multipart/form-data"属性,就没办法完成上传图片动作,但是可以获取文本值。
我只好把两个动作分开写到两个表单中,但是我想制作成一起的,点击一个提交两个动作同时完成。请指教,谢谢。
2 回复
#2
海风3082008-11-07 18:04
注:我只在数据库中保存图片路径
#3
wyq032008-11-07 18:15
jsp:
<form id="fileUploadForm" name="fileUploadForm" action="./wyqService.action"
        enctype="multipart/form-data" method="post">
    <input type="file" name="file" id="file" size="40"/><br>
    <input type="submit" name="uploadButton" id="uploadButton" value="开始上传"/>
    <input type="button" name="cancelUploadButton" id="cancelUploadButton" value="取消上传"/><br>
</form>
action:
        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        try {
            // 上传在临时目录的临时文件
            FileInputStream fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);

            // 指定的文件存放地点
            FileOutputStream fos = new FileOutputStream(new File(dir,
                    getFileFileName()));
            bos = new BufferedOutputStream(fos);

            // 转移文件
            byte[] buf = new byte[4096];
            int len = -1;
            while ((len = bis.read(buf)) != -1) {
                bos.write(buf, 0, len);
            }

            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
            uploadary.add(getFileFileName() + "上传成功!");
        } catch (Exception e) {
            e.printStackTrace();
            uploadary.add(getFileFileName() + "上传失败!");
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
1