I have used file-upload
(common fileuplod) in strtus-2.3.15.3
.
There is one Form in my .jsp
with multiple field with many diff type(textfield,textarea,hidden,file) including FILE
and obvious SUBMIT
.
When i submit form by selecting one file and enter some text in all other field its generated .tmp
file in mention temp folder . Only .tmp
file related to file field is going to delete after uploading my file to myfolder but rest of .tmp
(with 1kb size) file is remaing as its .
List items = upload.parseRequest(servletRequest);
This line in below code generate .tmp
file for all field which have some value (if you do not enter any text in text field it not generated) .
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> Why this .tmp
file is generated for fields (textarea ,hidden,textfield) .
How can we prevent this ?
2> i want to stop generating .tmp
file for all field except where type='file'
(file field) .
3> Otherwise, how can i delete this all .tmp
file ?
You don't need Commons libraries for this, nor a Servlet.
You are using Struts2, so don't reinvent the wheel and use Actions and Interceptors.
You can find the code to upload multiple files with Struts2 in this exhaustive answer , and a little improvement by creating a custom object in this other answer.
I feel the need to link this nice answer from BalusC too, when talking about fileUpload through Servlet.
Let's come to your specific question: you are using a
MonitoredDiskFileItemFactory
, (you didn't specified which of the many implementations growing on the web, but it is likely that it is ->) a Subclass of the standardorg.apache.commons.fileupload.disk.DiskFileItemFactory
.In the JavaDoc it is well explained that:
From Commons FileUpload Documantation
Then you can: