浏览 4006 次
锁定老帖子 主题:ssh上传图片
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-06-25
需要在webroot下建一个upload的文件夹,上传之后可以在你的WEB服务器,你的工程里面看到你上传的图片。 这是一个action package com.song.action; 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.song.form.FileUploadAndSaveForm; import java.io.*; public class FileUploadAndSaveAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { FileUploadAndSaveForm myForm = (FileUploadAndSaveForm) form; FormFile myFile = myForm.getTheFile(); String contentType = myFile.getContentType(); //Get the file name String fileName = myFile.getFileName(); //String fileName = request.getParameter("id"); //int fileSize = myFile.getFileSize(); byte[] fileData = myFile.getFileData(); //Get the servers upload directory real path name String filePath = getServlet().getServletContext().getRealPath("/") + "upload"; if (!fileName.equals("")) { System.out.println("Server path:" + filePath); //Create file File fileToCreate = new File(filePath, fileName); //If file does not exists create file if (!fileToCreate.exists()) { FileOutputStream fileOutStream = new FileOutputStream( fileToCreate); fileOutStream.write(myFile.getFileData()); fileOutStream.flush(); fileOutStream.close(); } } //Set file name to the request object request.setAttribute("fileName", fileName); System.out.println(fileName); return mapping.findForward("success"); } } form package com.song.form; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; public class FileUploadAndSaveForm extends ActionForm { private FormFile theFile; public FormFile getTheFile() { return theFile; } public void setTheFile(FormFile theFile) { this.theFile = theFile; } public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub return null; } public void reset(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub } } 下面是两个JSP页面 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body bgcolor="white"> <form action="fileUploadAndSave.do" method="post" enctype="multipart/form-data" > <input type="hidden" name="id" value="0002.jpg"> File Name <input type="file" name="theFile"> <input type="submit" value="submit"> </form> </body> <html> <head> <title>Success</title> </head> <body> <% String fileName=(String)request.getAttribute("fileName"); %> <p align="center"><font size="5" color="#000080">File Successfully Received</font></p> <p align="center"><a href="upload/<%=fileName%>">Click here to download</a></p> <img src="upload/<%=fileName%>"> </body> </html> 配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans > <form-bean name="fileUploadAndSaveForm" type="com.song.form.FileUploadAndSaveForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action path="/fileUploadAndSave" type="com.song.action.FileUploadAndSaveAction" name="fileUploadAndSaveForm" scope="request" validate="true" input="/FileUploadAndSave.jsp"> <forward name="success" path="/downloaduploadedfile.jsp"/> </action> </action-mappings> <message-resources parameter="com.song.ApplicationResources" /> </struts-config> 以上的程序是经过我的测试的 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |