public class UploadFileAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UploadFileForm theForm = (UploadFileForm) form;
//取得上传的文件
FormFile file = theForm.getFiles();
try {
//把文件读入
InputStream stream = file.getInputStream();
//上传到指定的upload包中
String filePath = request.getRealPath("/");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//建立一个上传文件的输出流
OutputStream bos = new FileOutputStream(filePath + "/"
+ file.getFileName());
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();
} catch (Exception e) {
System.err.print(e);
}
return mapping.findForward("success");
}
}
import org.apache.struts.upload.FormFile;
public class UploadFileForm extends ActionForm {
private FormFile files;
/**
* @return the files
*/
public FormFile getFiles() {
return files;
}
/**
* @param files the files to set
*/
public void setFiles(FormFile files) {
this.files = files;
}
}
以上是一个文件的上传,如果是多个的上传的话,就要把ACTION中的方法如下修改
//获得上传的文件信息
Hashtable fileh = form.getMultipartRequestHandler().getFileElements();
String dirPath = getServlet().getServletContext().getRealPath("/");
if (fileh != null) {
// 如果目录不存在,则创建
File fpath = new File(dirPath);
if (!fpath.exists()) {
fpath.mkdir();
}
// 解析上传文件
for (Iterator it = fileh.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
try {
FormFile formfile = (FormFile) fileh.get(key);
String filename = formfile.getFileName().trim(); // 文件名
if (!"".equals(filename)) {
InputStream ins = formfile.getInputStream();
OutputStream os = new FileOutputStream(dirPath
+ File.separatorChar + filename);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UploadFileForm theForm = (UploadFileForm) form;
//取得上传的文件
FormFile file = theForm.getFiles();
try {
//把文件读入
InputStream stream = file.getInputStream();
//上传到指定的upload包中
String filePath = request.getRealPath("/");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//建立一个上传文件的输出流
OutputStream bos = new FileOutputStream(filePath + "/"
+ file.getFileName());
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();
} catch (Exception e) {
System.err.print(e);
}
return mapping.findForward("success");
}
}
import org.apache.struts.upload.FormFile;
public class UploadFileForm extends ActionForm {
private FormFile files;
/**
* @return the files
*/
public FormFile getFiles() {
return files;
}
/**
* @param files the files to set
*/
public void setFiles(FormFile files) {
this.files = files;
}
}
以上是一个文件的上传,如果是多个的上传的话,就要把ACTION中的方法如下修改
//获得上传的文件信息
Hashtable fileh = form.getMultipartRequestHandler().getFileElements();
String dirPath = getServlet().getServletContext().getRealPath("/");
if (fileh != null) {
// 如果目录不存在,则创建
File fpath = new File(dirPath);
if (!fpath.exists()) {
fpath.mkdir();
}
// 解析上传文件
for (Iterator it = fileh.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
try {
FormFile formfile = (FormFile) fileh.get(key);
String filename = formfile.getFileName().trim(); // 文件名
if (!"".equals(filename)) {
InputStream ins = formfile.getInputStream();
OutputStream os = new FileOutputStream(dirPath
+ File.separatorChar + filename);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}