[Struts2] 文件上传成功但是出现java.lang.IllegalStateException异常
struts.xml
<action name="savecompany" class="companyAction"
method="saveCompany">
<!-- Struts2内置的文件上传拦截器 -->
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/bmp,image/gif,image/JPEG,image/pjpeg,image/jpg
</param>
<param name="maximumSize">2048000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="success">/operations.jsp</result>
</action>
action:
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;
}
上传文件的Function:
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;
}
}
相关推荐
tomcat部署多个项目.pdf
问题 1:java.lang.IllegalStateException在多个项目中,可能会遇到 `java.lang.IllegalStateException` 异常,例如:`java.lang.IllegalStateException
java的常见问题及解决方法定义.pdf
**HQL查询语法错误**: 如果遇到“java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.IdentNode
广东屈臣氏饮料制造项目-全专业-20240508.nwd
广东屈臣氏饮料制造项目_全专业-20240508.nwd
Python3 if多条件简写技巧
Python3支持极简if条件写法,简化冗余代码。链式比较:原生支持1<x<10,无需写成x>1 and x<10,可读性更强。三元表达式:适用于二分支逻辑,语法 结果A if 条件 else 结果B,适合单行赋值,禁止多层嵌套三元表达式,会大幅降低可读性。逻辑短路:and左侧为False直接终止判断,or左侧为True直接终止判断,可用于空值兜底,比如name = input_name or "匿名用户"。注意不要混用=和==,新手常误将赋值写在if判断内,Python3会直接抛出语法异常。 6thhosp-east.com 3wfuwu.com ajfcw.net www.ahdmhbkj.cn www.aiyouseo.com
PHP 代码加密工具 源码保护与站点授权
把 PHP 源码加密成不可直接阅读、难以还原的形式:变量名与控制流被打散重写,载荷带完整性自校验,改一个字节就停止运行。提供编码型 / 混淆型 / 重构型 / 底层型 / 虚拟机型五档保护强度,并可叠加按域名与期限的网站注册授权。


Web 开发
