我在用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;
}
}

解决方案 »

  1.   

    我见网上的网友都没有出现这样的问题,真怪,我用的是struts2.1.8, 相关的jar包都在里面lib下找的
      

  2.   

    确实没出现过这样的问题.!  不明白你textfield的东西是真么传到哪个目录下去的
      

  3.   

    action的属性中关于上传文件的有命名规范要求:
        private File myFile;
        private String myFileContentType;
        private String myFileFileName;
      

  4.   

    我也遇到了这个问题。上传了文件以后,会出现好几个temp文件。eclipse总是报错说不能删除temp文件,一个应用程序正在使用。
    我用的也是struts2.1.8
    我说一个我的解决方案struts.xml配置一个temp文件夹
    <constant name="struts.multipart.saveDir" value="/tmp"></constant>