我file-upload
在strtus-2.3.15.3
. 我的表单中.jsp
有多个字段,其中包含许多差异类型(文本字段、文本区域、隐藏、文件),包括FILE
和明显SUBMIT
的 .
当我通过选择一个文件提交表单并在所有其他字段中输入一些文本时,它.tmp
在提及临时文件夹中生成的文件。将我的文件上传到 myfolder 后,只有.tmp
与文件字段相关的文件才会删除,但其余.tmp
(1kb 大小)文件仍保留为 .
List items = upload.parseRequest(servletRequest);
下面代码中的这一行为.tmp
所有具有某些值的字段生成文件(如果您没有在它未生成的文本字段中输入任何文本)。
MonitoredMultiPartRequest.java
:
public void parse(HttpServletRequest servletRequest, String saveDir)
throws IOException
{
System.setProperty("java.io.tmpdir", "D:\\ankit");
UploadListener listener = new UploadListener(servletRequest);
// Create a factory for disk-based file items
FileItemFactory factory = new MonitoredDiskFileItemFactory(listener);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
}
MonitoredDiskFileItemFactory
:
public class MonitoredDiskFileItemFactory extends DiskFileItemFactory
{
HttpServletRequest request;
public MonitoredDiskFileItemFactory(OutputStreamListener listener, HttpServletRequest request)
{
this.listener = null;
this.listener = listener;
this.request = request;
setTrackers();
}
public void setTrackers()
{
FileCleaningTracker fileCleaningTracker = FileCleanerCleanup.getFileCleaningTracker(request.getServletContext());
File repository = new File(System.getProperty("java.io.tmpdir"));
DiskFileItemFactory factory = new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, repository);
factory.setFileCleaningTracker(fileCleaningTracker);
super.setFileCleaningTracker(fileCleaningTracker);
super.setRepository(repository);
}
public MonitoredDiskFileItemFactory(int sizeThreshold, File repository, OutputStreamListener listener)
{
super(sizeThreshold, repository);
this.listener = null;
this.listener = listener;
}
public FileItem createItem(String fieldName, String contentType, boolean isFormField, String fileName)
{
MonitoredDiskFileItem result = new MonitoredDiskFileItem(fieldName, contentType, isFormField, fileName, getSizeThreshold(), getRepository(), listener);
FileCleaningTracker tracker = getFileCleaningTracker();
if (tracker != null)
{
tracker.track(result.getTempFileOfDistFileItem(), result);
}
return result;
}
private OutputStreamListener listener;
}
MonitoredDiskFileItem
:
public class MonitoredDiskFileItem extends DiskFileItem
{
public MonitoredDiskFileItem(String fieldName, String contentType, boolean isFormField, String fileName, int sizeThreshold, File repository, OutputStreamListener listener)
{
super(fieldName, contentType, isFormField, fileName, sizeThreshold, repository);
mos = null;
this.listener = listener;
}
public OutputStream getOutputStream()
throws IOException
{
if (mos == null)
mos = new MonitoredOutputStream(super.getOutputStream(), listener);
return mos;
}
public File getTempFileOfDistFileItem()
{
return super.getTempFile();
}
private MonitoredOutputStream mos;
private OutputStreamListener listener;
}
UploadListener
:
public class UploadListener implements OutputStreamListener, Serializable
{
private static final long serialVersionUID = 1L;
private int totalToRead = 0;
private int totalBytesRead = 0;
private int percentDone = 0;
private int previou_percentDone = 0;
private long uploadspeed = 0;
private long starttime;
private long stTime, EndTime;
HttpSession session;
private int count = 0;
public UploadListener(HttpServletRequest request)
{
totalToRead = request.getContentLength();
session = request.getSession();
}
public void start()
{
session.setAttribute("percentageDone", 0);
session.setAttribute("speed", 0);
starttime = System.currentTimeMillis();
stTime = starttime;
}
public String getMessage()
{
return "" + totalBytesRead + " bytes have been read (" + percentDone + "% done) ";
}
public void bytesRead(int bytesRead)
{
totalBytesRead = totalBytesRead + bytesRead;
if (100.00 * totalBytesRead > totalToRead)
{
previou_percentDone = percentDone;
percentDone = (int) Math.round(100.00 * totalBytesRead / totalToRead);
if (previou_percentDone < percentDone)
{
long speed = 0;
try
{
double TimediffInSecond = (System.currentTimeMillis() - starttime) / 1000;
if (TimediffInSecond > 0)
speed = Math.round(((totalBytesRead) / TimediffInSecond) / 1048576);
else
speed = totalBytesRead / 1048576;
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
}
}
}
public void done()
{
EndTime = System.currentTimeMillis();
session.setAttribute("percentageDone", 100);
session.setAttribute("speed", 100);
}
@Override
public void error(String message)
{
// System.out.println(message);
}
public long getUploadspeed()
{
return uploadspeed;
}
public void setUploadspeed(long uploadspeed)
{
this.uploadspeed = uploadspeed;
}
}
EDIT
:
1> 为什么.tmp
为字段(textarea,hidden,textfield)生成此文件。我们怎样才能防止这种情况发生?
2> 我想停止.tmp
为除 where type='file'
(file field) 之外的所有字段生成文件。
3>否则,我该如何删除所有.tmp
文件?