nojave 2009-11-30 00:36
浏览 345
已采纳

文件上传出现java.lang.IllegalStateException

用 上传文件,可以正确上传,但是最后出现
[code="java"]
exception

java.lang.IllegalStateException
org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:725)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)

[/code]
直接贴代码了
struts.xml
[code="xml"] method="saveCompany">
<!-- Struts2内置的文件上传拦截器 -->


image/bmp,image/gif,image/JPEG,image/pjpeg,image/jpg

2048000


/operations.jsp

[/code]

action:

[code="java"]
public String saveCompany() {
System.out.println("upload file");
// upload file
HttpServletRequest request = ServletActionContext.getRequest();
// Get real path
String realpath = request.getSession().getServletContext().getRealPath(
"/");
//save path is a directory
String savepath = realpath + "images\" + this.getId();
String filetype = this.getLogoContentType();
//create file type
filetype = filetype.replace("image/", "");
filetype = filetype.replace("pjpeg", "jpg");

    try {
        if (CreatePath(savepath) == false) {
            throw new SystemException("创建文件夹失败,");
        } else {
            //file = save path + logo. + file type 
            String savefile = savepath + "\\" + "logo." + filetype;
            File tarFile = new File(savefile);
            System.out.println(savefile);
            fu.uploadFile(this.getLogo(), tarFile);
        }
    } catch (Exception e) {
        throw new SystemException("文件上传失败,");
    }
    return SUCCESS;
}

[/code]

上传文件的Function:
[code="java"]
package com.prodinfo.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.opensymphony.xwork2.ActionSupport;

public class FileUpload extends ActionSupport implements IFileUpload {

private static final int BUFFER_SIZE = 2048000;

public String uploadFile(File src, File target) {
    try {
        FileOutputStream fileout = new FileOutputStream(target);
        FileInputStream filein = new FileInputStream(src);
        byte[] buffer = new byte[BUFFER_SIZE];
        int len = 0;
        while ((len = filein.read(buffer)) > 0) {
            fileout.write(buffer, 0, len);
        }
        fileout.close();
        filein.close();
    } catch (Exception e) {
        throw new SystemException("文件上传失败,");
    }
    return SUCCESS;
}

}
[/code]
[b]问题补充:[/b]
感谢hearken01兄回答.
1,2,3点我确定应该是无问题的.
至于4点,不是很明白如何设为property属性,不知道hearken01兄是否可以解释下.
附上jsp代码:
[code="xml"]

公司代码:
                        <label class="contact">
                            <strong>公司Logo:</strong>
                        </label>
                        <s:file name="logo" />
                        <div class="form_row">
                            <s:submit value="保存"></s:submit>
                            <s:reset value="重置"></s:reset>
                        </div>
                    </s:form>
                </div>

[/code]

  • 写回答

2条回答 默认 最新

  • xkuff 2009-11-30 10:29
    关注

    [code="java"]
    // 在ActionForm Bean中设置FormFile属性
    /**
    *The file that the user has uploaded
    */
    private FormFile file;
    public FormFile getFile(){
    return this.file;
    }
    public void setFile(FormFile file){
    this.file file;
    }

    // 在Action类中处理文件上传
    String dir = servlet.getServletContext().getRealPath("/");
    HtmlFileForm hff = (HtmlFileForm)form;

    FormFile file = hff.getFile();

    if(file == null)
    return mapping.findForward("error");

    String fname = file.getFileName();
    String size = Integer.toString(file.getFileSize())+"bytes";

    InputStream streamIn = file.getInputStream();
    OutputStream streamOut = new FileOutputStream(dir+"/"+fname);

    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    while((bytesRead = streamIn.read(buffer,0,8192)) != -1){
    streamOut.write(buffer,0,bytesRead);
    }

    streamOut.clase();
    streamIn.close();
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度