package com.test.struts.action;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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.action.DynaActionForm;
import org.apache.struts.upload.FormFile;/**
* MyEclipse Struts
* Creation date: 07-05-2006
*
* XDoclet definition:
* @struts.action path="/upfile" name="upfileForm" input="/upfile.jsp" scope="request" validate="true"
*/
public class UpfileAction extends Action { // --------------------------------------------------------- Instance Variables // --------------------------------------------------------- Methods /**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DynaActionForm upfileForm = (DynaActionForm) form;
// 声明并获取对像
String filename = upfileForm.getString("filename");
// 输出文件名
System.out.println(filename);
FormFile filedata = (FormFile) upfileForm.get("filedata");
// 取当前系统路径
String filePath = request.getRealPath("/");
try {
// 转换文件为数据流
InputStream stream = filedata.getInputStream();
// 建立输出流
OutputStream bos = new FileOutputStream(filePath + "/" +
filedata.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 (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 返回到提交页面
return mapping.getInputForward();
}}
import java.io.FileOutputStream;
import java.io.IOException;
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.action.DynaActionForm;
import org.apache.struts.upload.FormFile;/**
* MyEclipse Struts
* Creation date: 07-05-2006
*
* XDoclet definition:
* @struts.action path="/upfile" name="upfileForm" input="/upfile.jsp" scope="request" validate="true"
*/
public class UpfileAction extends Action { // --------------------------------------------------------- Instance Variables // --------------------------------------------------------- Methods /**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DynaActionForm upfileForm = (DynaActionForm) form;
// 声明并获取对像
String filename = upfileForm.getString("filename");
// 输出文件名
System.out.println(filename);
FormFile filedata = (FormFile) upfileForm.get("filedata");
// 取当前系统路径
String filePath = request.getRealPath("/");
try {
// 转换文件为数据流
InputStream stream = filedata.getInputStream();
// 建立输出流
OutputStream bos = new FileOutputStream(filePath + "/" +
filedata.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 (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 返回到提交页面
return mapping.getInputForward();
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货