在Action里面取得路径:
String pathMyDir = this.getServlet().getServletContext().getRealPath("/uploads/");
File targetFolder = new File(pathMyDir);
if (targetFolder.isFile() || !targetFolder.exists())
{
targetFolder.mkdir();
}
为避免重复文件名
FormFile file = theForm.getTheFile();
String fileName= file.getFileName();
int i = fileName.lastIndexOf(".");
if(i != -1)
{
fileName = fileName.substring(i);
}else
{
fileName = ".bin";
}
fileName = new Date().getTime()+fileName;
最后别忘了file.destroy();
其他的就用struts里面自带的例子搞定.
InputStream stream = file.getInputStream();
OutputStream bos = new FileOutputStream(pathMyDir+fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
stream.close();
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=250969