ssh上传图片

本文介绍了一个基于Struts框架实现的图片上传示例项目。项目通过Action类处理图片上传请求,并将图片保存到服务器指定目录。文章提供了完整的代码实现,包括Action类、Form类及JSP页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明这是一个ssh集成项目中使用Struts做的上传图片的demo,数据库中存储的是上传图片的名称

需要在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>


以上的程序是经过我的测试的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值