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

📄 uploadaction.java

📁 在STRUTS框下实现文件上传
💻 JAVA
字号:
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package struts.action;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.ServletContext;
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 struts.form.UpLoadForm;

/** 
 * MyEclipse Struts
 * Creation date: 08-24-2008
 * 
 * XDoclet definition:
 * @struts.action path="/upLoad" name="upLoadForm" input="upLoad.jsp" scope="request" validate="true"
 */
public class UpLoadAction extends Action
{
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UpLoadForm upLoadForm = (UpLoadForm) form;// TODO Auto-generated method stub
		FormFile myFile=upLoadForm.getMyFile();
		//得到文件基本信息
		String type=myFile.getContentType();//得到文件类型
		String FileName=myFile.getFileName();//得到文件名称
		int size=myFile.getFileSize();//得到文件大小
		System.out.println("类型"+type);
		System.out.println("名称"+FileName);
		System.out.println("大小"+size);
		//把文件保存到服务器
		FileOutputStream fos=null;
		try {
			byte[] data=myFile.getFileData();
			//java如果要访问服务器硬盘,一定要提供逻辑路径(B硬盘上的路径C:\....)
			// 应用默认在Tomcat 下的Bin目录下
			//怎样将URL相对路径/FILES/转成硬盘上的绝对路径?用application
			ServletContext application=this.getServlet().getServletContext();
			String realPath=application.getRealPath("/Files/");
			 fos=new FileOutputStream(realPath+"/"+FileName);
			 System.out.println("路径是:"+realPath);
			 fos.write(data);
		} 
		 catch (Exception e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		 finally {
			 try {
				 fos.close();
			 }
			 catch (Exception e) {
				// TODO: handle exception
			}
		 }
		 try {
			 /*得到上传文件
			InputStream is=myFile.getInputStream();//得到上传的内容。可以对文件进行分析
		    BufferedReader br=new BufferedReader(new InputStreamReader(is));
			*/
			
		}catch (Exception e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	
		
	return new ActionForward("/upLoad.jsp");
	}
}

⌨️ 快捷键说明

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