分类: Java
2010-03-25 09:58:48
package com.htmltaglibs.struts.action;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import com.htmltaglibs.struts.form.HtmlFileForm;
/**
* MyEclipse Struts Creation date: 03-24-2010
*
* XDoclet definition:
*
* @struts.action path="/htmlFile" name="htmlFileForm"
* input="/form/htmlFile.jsp" scope="request" validate="true"
* @struts.action-forward name="success" path="/HtmlFile.jsp"
*/
public class HtmlFileAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HtmlFileForm hff = (HtmlFileForm) form;
// String dir = servlet.getServletContext().getRealPath("/upload");
String dir = "e:/upload";
FormFile file = hff.getFile();
if (file == null) {
return mapping.findForward("success");
}
String fname = file.getFileName();
String size = Integer.toString(file.getFileSize()) + "bytes";
InputStream istream = file.getInputStream();
// OutputStream ostream = new FileOutputStream(new String((dir + "/" + fname).getBytes("GBK"),"UTF-8"));
OutputStream ostream = new FileOutputStream(dir + "/" + (new String(fname.getBytes("GBK"),"gb2312")));
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = istream.read(buffer, 0, 8192)) != -1) {
ostream.write(buffer, 0, bytesRead);
}
istream.close();
ostream.close();
hff.setFname(new String(fname.getBytes("GBK"),"gb2312"));
hff.setSize(size);
file.destroy();
return mapping.findForward("success");
}
}