JSP页面<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>uploadfiles.jsp</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>
<form action="uploadfile.do?action=addfile"enctype="multipart/form-data" method="post">
<table align="center" width="200px">
<tr>
<td nowrap="nowrap">
文件名:
</td>
<td>
<input type="file" name="filename" />
</td>
</tr>
<tr>
<td nowrap="nowrap">
备注
</td>
<td>
<input type="textarea" name="memo" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="增加资料"/>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>uploadfiles.jsp</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>
<form action="uploadfile.do?action=addfile"enctype="multipart/form-data" method="post">
<table align="center" width="200px">
<tr>
<td nowrap="nowrap">
文件名:
</td>
<td>
<input type="file" name="filename" />
</td>
</tr>
<tr>
<td nowrap="nowrap">
备注
</td>
<td>
<input type="textarea" name="memo" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="增加资料"/>
</td>
</tr>
</table>
</form>
</body>
</html>
解决方案 »
- JNative 调用dll的问题
- Cannot instantiate class: org.jnp.interfaces.NamingContextFactory
- 被这个问题折腾了1天了,求教高手.
- jsp 实现动态树
- web service 调用dll的问题
- oracle bpel console
- Spring+Hibernate不解求解
- hibernate 持久化对象的一对多检索策略问题 100分!
- 请大家推荐几本书吧!
- 求助:关于struts标签里的optionsCollection错误
- 大家都用什么方式使用代码形式限制开发人员变成行为,让程序可维护度高
- 请问几个 Java 问题,还望各位多指点!
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.struts.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;
import org.apache.struts.upload.MultipartRequestHandler;/**
* MyEclipse Struts Creation date: 01-16-2008
*
* XDoclet definition:
*
* @struts.form name="uploadfileForm"
*/
public class UploadfileForm extends ActionForm { private String memo; private FormFile filename = null; public FormFile getFilename() {
return filename;
} public void setFilename(FormFile filename) {
this.filename = filename;
} public String getMemo() {
return memo;
} public void setMemo(String memo) {
this.memo = memo;
} public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if(filename==null){
System.out.println("filename为null");
}
return errors;
} public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
}
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.struts.action;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;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.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;
import com.struts.form.UploadfileForm;import com.struts.DAO.FileDAO;/**
* MyEclipse Struts Creation date: 01-16-2008
*
* XDoclet definition:
*
* @struts.action path="/uploadfile" name="uploadfileForm" input="/seeload.jsp"
* scope="request" validate="true"
*/
public class UploadfileAction extends Action {
/*
* Generated Methods
*/ /**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String encoding = request.getCharacterEncoding();
if((encoding != null)&&(encoding.equalsIgnoreCase("utf-8"))){
response.setContentType("text/html;charset=gb2312");// 如果没有指定编码,编码格式为gb2312
}
UploadfileForm uploadfileForm = (UploadfileForm) form;
FormFile filename = uploadfileForm.getFilename();
String memo = uploadfileForm.getMemo();
DataSource ds = null;
Connection conn = null;
HttpSession session = null;
try{
ds = getDataSource(request,"A");
conn = ds.getConnection();
FileDAO sqlDao = new FileDAO(conn);
String action = request.getParameter("action");
ActionMessages errors = new ActionMessages();
// 增加文件
if(action.equals("addfile")){
// 传递文件
if(filename.getFileName().trim().length()>0){
// 取得上传时间
SimpleDateFormat DateFormat1 = new SimpleDateFormat("yyy-MM-dd");
Date dt = new Date();
String uptime = DateFormat1.format(dt);
String tempname="";
try{
// 开始上传文件
String filePath = this.getServlet().getServletContext().getRealPath("/");// 取得当前路径
InputStream stream = filename.getInputStream();// 把文件读入
// 建立一个上传文件的输入流如果是linux系统请把"\"换成"/"
System.out.println(filename.getFileName());
int position = filename.getFileName().indexOf(".");
String type = filename.getFileName().substring(position);
String fileid = sqlDao.getMaxFileID();
tempname = fileid + type;
OutputStream bos = new FileOutputStream(filePath + "upfiles"+"'\'" + tempname);
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(Exception e){
errors.add(ActionMessages.GLOBAL_MESSAGE , new ActionMessage("file.add.failure")) ;
if ( !errors.isEmpty()){
saveErrors(request, errors) ;
}
return new ActionForward(mapping.getInput());
}
//写入到数据库记录
if(!sqlDao.inserNew(tempname, filename.getFileName(), memo, uptime)){
errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("file.add.failure"));
if(!errors.isEmpty()){
saveErrors(request,errors);
}
return new ActionForward(mapping.getInput());
}
removeFormBean(mapping,request);
return new ActionForward(mapping.getInput());
}
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
return null;
} protected void removeFormBean(ActionMapping mapping,
HttpServletRequest request) {
if (mapping.getAttribute() != null)
if ("request".equals(mapping.getScope())) {
request.removeAttribute(mapping.getAttribute());
} else {
HttpSession session = request.getSession();
session.removeAttribute(mapping.getAttribute());
}
}}
<!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="uploadfileForm" type="com.struts.form.UploadfileForm" /> </form-beans> <global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="uploadfileForm"
input="/uploadfiles.jsp"
name="uploadfileForm"
path="/uploadfile"
scope="request"
type="com.struts.action.UploadfileAction" >
<forward name="listfiles" path="/seeload.jsp" />
</action>
</action-mappings> <message-resources parameter="com.struts.ApplicationResources" />
</struts-config>