⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 touaction.java

📁 在jsp中使用smartupload组件上传文件
💻 JAVA
字号:
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 touAction 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");
		// 取当前系统路径
		 if(filedata!=null && filedata.getFileName()!= null &&filedata.getFileName().trim().length()!=0){   
			 String filePath = request.getRealPath("/");
			 String   fname   =   filedata.getFileName();   
			 String   f  =filename+fname.substring(fname.indexOf("."));	
		try {
			// 转换文件为数据流
			InputStream stream = filedata.getInputStream();
			// 建立输出流
			OutputStream bos = new FileOutputStream(filePath + "/" + f);
			// 将文件写入网站根目录下
			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();
		}
		 }else{   
			  //errors.add("fileRequired",   new   ActionError("error.bulletin.fileRequired"));   
			  }
		// 返回到提交页面
		return mapping.getInputForward();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -