我这是从自己写的项目里拷出来的啊!看不明白的可以问我!
(1)jsp:
<%@ page contentType="text/html; charset=utf-8" pageEncoding="GB2312" language="java" %>
<%@ include file="/WEB-INF/view/tiles/layout-content-include.jsp" %>
<%@ page import="java.util.HashMap;"%>
<html>
<head>
<title></title>
<script language="javaScript">
function add_LEDScreenSet(){
if(checkUserInput()){
if (!window.confirm("确定保存文件吗?")){
return false;
}else{
window.location="save_LEDScreenSet.do";
return true;
}
}
return false;
}
function checkUserInput(){
if (isEmpty(document.all.name.value)){
alert("中文描述不能为空!");
document.all.name.focus();
return false;
}
if (isEmpty(document.all.formFile.value)){
alert("上传文件不能为空!");
document.all.formFile.focus();
return false;
}
if(!isEmpty(document.all.formFile.value)){
var url=document.all.formFile.value;
var i =url.lastIndexOf('.');
var test = url.substring(i+1).toUpperCase().replace(' ','');
if(test=="JPG"||test=="JPEG" || test=="GIF"|| test=="BMP" )
{
return true;
}else{
alert("格式不正确,只能是 BMP,GIF,JPEG,JPG 格式的图片!");
return false;
}
}
return true;
}
</script>
</head>
<body>
<br />
<html:form action="/save_LEDScreenSet" enctype="multipart/form-data" method="post" onsubmit="return add_LEDScreenSet();">
<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0" >
<html:hidden property="id"/>
<tr><td>中文描述:<html:text property="name"/></td></tr>
<tr><td>添加图片:<html:file property="formFile"/></td>
</tr>
<tr>
<td>
<html:submit value="上传"/>
<input name="cancel" type="button" value="取消"
onclick="history.back();">
</td>
</tr>
</table>
</html:form>
</body>
</html>
(2)action 处理保存到服务指定的目录下
import org.apache.struts.upload.FormFile;
public ActionForward save_led(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LEDScreenSetForm ledf = (LEDScreenSetForm) form;
// 可以用来处理多个文件的上传
logger.info("inter ledScreenSet save!");
FormFile file = ledf.getFormFile(); // 处理得到上传的文件
String dir = servlet.getServletContext().getRealPath(
"//resource//image//content");
if (!dir.endsWith("/"))
dir = dir.concat("/");
String fname = file.getFileName();
ledf.setPath("E:/image/" + fname);
InputStream in = null;
OutputStream out = null;
try {
in = file.getInputStream();
out = new FileOutputStream(dir + fname);
int byteread = 0;
byte[] bytes = new byte[10000];
while ((byteread = in.read(bytes, 0, 10000)) != -1) {
out.write(bytes, 0, byteread);
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
in.close();
out.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
if (ledScreenSetManagMr.insertLEDScreenSet(ledf)) {
request.setAttribute("message", "成功保存门禁LED屏幕设置!");
request.setAttribute("redirect", "main_LEDScreenSet.do");
} else {
request.setAttribute("message", "门禁LED屏幕设置保存失败!");
request.setAttribute("redirect", "add_LEDScreenSet.do");
}
return mapping.findForward("succeed");
}
(3)对应的form
package com.prg.safety.control.business.LEDScreenSet.form;
import org.apache.struts.upload.FormFile;
import org.apache.struts.action.ActionForm;
import com.prg.safety.model.business.LEDScreenSet.api.LEDScreenSetManager;
public class LEDScreenSetForm extends ActionForm implements LEDScreenSetManager{
private static final long serialVersionUID = 1L;
Integer id ;
String name;
String path ;
FormFile formFile;
public Integer getId() {
// TODO Auto-generated method stub
return id;
}
public String getName() {
// TODO Auto-generated method stub
return name;
}
public String getPath() {
// TODO Auto-generated method stub
return path;
}
public void setId(Integer id) {
// TODO Auto-generated method stub
this.id=id;
}
public void setName(String name) {
// TODO Auto-generated method stub
this.name=name;
}
public void setPath(String path) {
// TODO Auto-generated method stub
this.path=path;
}
public FormFile getFormFile() {
return formFile;
}
public void setFormFile(FormFile formFile) {
this.formFile = formFile;
}
}