我在用struts2上传文件, 发现上传单一文件时时在临时文件夹目录下会生产几个临时文件,
但只有上传的文件对应的临时文件被自动删除,剩下的是以下很小的表单内容文件,
却不会自动删除。(如form里面有file表单和一个textfield表单,上传时会生产2个临时文件f.tmp,和t.tmp,
上传成功后f.tmp会被删除,而t.tmp则一直存在,其内容是textfield的value的值
)struts.xml配置如下<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="UTF8"></constant>
<constant name="struts.multipart.saveDir" value="d:/tmp"></constant>
<package name="fileUploadDemo" extends="struts-default">
<action name="fileUpload"
class="test.struts2.fileupload.action.FileUploadAction">
<interceptor-ref name="fileUpload" >
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png,image/pjpeg</param>
<param name="maximumSize">1024000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<!-- 上传文件存放文件夹 -->
<!-- <param name="savePath">d:/upload</param> -->
<result name ="input" > /FileUpload.jsp </result >
<result name="success">/ShowUpload.jsp</result>
</action>
</package>
</struts> web.xml配置如下:<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Struts 2 FileUpload</display-name>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>输入上传信息的jsp主体: <body>
<s:actionerror/>
<s:form action="fileUpload" method="post" enctype="multipart/form-data">
<s:file name="myFile" label="Image File"/>
<s:textfield name="caption" label="Caption" />
<s:submit value="上传"/>
</s:form>上传对应的action:package test.struts2.fileupload.action;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileUploadAction extends ActionSupport{ private static final long serialVersionUID = 6304065708203590590L;
private static final int BUFFER_SIZE = 16*1024;
private File myFile;
private String contentType;
private String fileName;
private String imageFileName;
private String caption;
private String savePath = null;
private static void copy(File src, File dest) {
InputStream is = null;
OutputStream os = null;
try {
is = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
os = new BufferedOutputStream(new FileOutputStream(dest), BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while(is.read(buffer) > 0) {
os.write(buffer);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(is != null) {
is.close();
}
if(os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
@Override
public String execute() {
imageFileName = new Date().getTime() + getExtention(fileName);
String dest = null;
dest = ServletActionContext.getServletContext().getRealPath("/UploadImages")
+ "/" + imageFileName;
File imageFile = new File(dest);
copy(myFile, imageFile);
return SUCCESS;
}
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public void setMyFileFileName(String fileName) {
this.setFileName(fileName);
}
public void setMyFileContentType(String contentType) {
setContentType(contentType);
} public void setSavePath(String savePath) {
this.savePath = savePath;
} public String getSavePath() {
return savePath;
}
}
但只有上传的文件对应的临时文件被自动删除,剩下的是以下很小的表单内容文件,
却不会自动删除。(如form里面有file表单和一个textfield表单,上传时会生产2个临时文件f.tmp,和t.tmp,
上传成功后f.tmp会被删除,而t.tmp则一直存在,其内容是textfield的value的值
)struts.xml配置如下<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="UTF8"></constant>
<constant name="struts.multipart.saveDir" value="d:/tmp"></constant>
<package name="fileUploadDemo" extends="struts-default">
<action name="fileUpload"
class="test.struts2.fileupload.action.FileUploadAction">
<interceptor-ref name="fileUpload" >
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png,image/pjpeg</param>
<param name="maximumSize">1024000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<!-- 上传文件存放文件夹 -->
<!-- <param name="savePath">d:/upload</param> -->
<result name ="input" > /FileUpload.jsp </result >
<result name="success">/ShowUpload.jsp</result>
</action>
</package>
</struts> web.xml配置如下:<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Struts 2 FileUpload</display-name>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>输入上传信息的jsp主体: <body>
<s:actionerror/>
<s:form action="fileUpload" method="post" enctype="multipart/form-data">
<s:file name="myFile" label="Image File"/>
<s:textfield name="caption" label="Caption" />
<s:submit value="上传"/>
</s:form>上传对应的action:package test.struts2.fileupload.action;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileUploadAction extends ActionSupport{ private static final long serialVersionUID = 6304065708203590590L;
private static final int BUFFER_SIZE = 16*1024;
private File myFile;
private String contentType;
private String fileName;
private String imageFileName;
private String caption;
private String savePath = null;
private static void copy(File src, File dest) {
InputStream is = null;
OutputStream os = null;
try {
is = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
os = new BufferedOutputStream(new FileOutputStream(dest), BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while(is.read(buffer) > 0) {
os.write(buffer);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(is != null) {
is.close();
}
if(os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
@Override
public String execute() {
imageFileName = new Date().getTime() + getExtention(fileName);
String dest = null;
dest = ServletActionContext.getServletContext().getRealPath("/UploadImages")
+ "/" + imageFileName;
File imageFile = new File(dest);
copy(myFile, imageFile);
return SUCCESS;
}
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public void setMyFileFileName(String fileName) {
this.setFileName(fileName);
}
public void setMyFileContentType(String contentType) {
setContentType(contentType);
} public void setSavePath(String savePath) {
this.savePath = savePath;
} public String getSavePath() {
return savePath;
}
}
解决方案 »
- java web project 问题?大神救助此问题?
- 新手请教:Ext+struts结合问题,如何在struts中获得radio选中值
- 关于jsp页面传递参数的问题
- 高薪诚聘(北京):中高级java工程师、资深高级java架构师
- 请问如何把网络上的一个目录下的所有文件放在自己程序中的一个Vector中
- 请教关于邮箱的问题
- 为什么只能插入一个表(hibernate 一对多)
- 怎么才能把内容加上xsd:string这样的类型??200分 help 谢谢~~~~~~~~~~~~~~~~~~``
- 请问rose中怎么导入一个jar包呢?
- 各位大侠!小弟刚接触ejb!请问ejb是什么?是java的什么方向的一部分?
- 使用SSH时遇到的问题
- 寻求一个复杂文档录入和显示的解决方案
private File myFile;
private String myFileContentType;
private String myFileFileName;
我用的也是struts2.1.8
我说一个我的解决方案struts.xml配置一个temp文件夹
<constant name="struts.multipart.saveDir" value="/tmp"></constant>