用 上传文件,可以正确上传,但是最后出现
[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]
[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:必须嵌套在标签中;
2:标签的method的属性必须设为"post";
3:标签的编码类型enctype属性必须为"multipart/form-data";
4:标签必须设为property属性,这个属性和ActionForm Bean中FormFile类型的属性对应。