以下是struts action代码,代码中使用struts1.1的FormFile来获得文件的各种信息
.............................
ResourceForm frm = (ResourceForm)form;
ResourceValue resource = new ResourceValue();
resource.setDescription( frm.getDescription() );
resource.setAuthor( frm.getAuthor() );
SimpleDateFormat fmt = new SimpleDateFormat( "yyyy.MM.dd HH:mm:ss" );
resource.setDt( fmt.format( new Date() ) );
FormFile file = frm.getContent();
resource.setContentType( file.getContentType() );
//If file is text/html file, set title
//else set title as filename
if( "text/html".equals( file.getContentType() ) )
{
resource.setTitle( frm.getTitle() );
}
else
{
String fname = file.getFileName();
String fext = fname.substring( fname.lastIndexOf( '.' ), fname.length() );
resource.setTitle( Long.toString( System.currentTimeMillis() ) + fext );
}
resource.setSize( file.getFileSize() + "bytes" );
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
//only write files out that are less than 1MB
if (file.getFileSize() < (1*1024000))
{
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
{
baos.write(buffer, 0, bytesRead);
}
resource.setContent(baos.toByteArray());
}
else
{
request.setAttribute( MESSAGE_KEY, "File is too greater" );
return mapping.findForward( ERROR );
}
//close the stream
stream.close();
getXstudio().createResource( resource );
Debug.print("Created resource: " + resource );
return mapping.findForward( SUCCESS );
}
catch (IOException ioe)
{
request.setAttribute( MESSAGE_KEY, "IOException:" + ioe.getMessage() );
return mapping.findForward( ERROR );
}
.................................
.............................
ResourceForm frm = (ResourceForm)form;
ResourceValue resource = new ResourceValue();
resource.setDescription( frm.getDescription() );
resource.setAuthor( frm.getAuthor() );
SimpleDateFormat fmt = new SimpleDateFormat( "yyyy.MM.dd HH:mm:ss" );
resource.setDt( fmt.format( new Date() ) );
FormFile file = frm.getContent();
resource.setContentType( file.getContentType() );
//If file is text/html file, set title
//else set title as filename
if( "text/html".equals( file.getContentType() ) )
{
resource.setTitle( frm.getTitle() );
}
else
{
String fname = file.getFileName();
String fext = fname.substring( fname.lastIndexOf( '.' ), fname.length() );
resource.setTitle( Long.toString( System.currentTimeMillis() ) + fext );
}
resource.setSize( file.getFileSize() + "bytes" );
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
//only write files out that are less than 1MB
if (file.getFileSize() < (1*1024000))
{
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
{
baos.write(buffer, 0, bytesRead);
}
resource.setContent(baos.toByteArray());
}
else
{
request.setAttribute( MESSAGE_KEY, "File is too greater" );
return mapping.findForward( ERROR );
}
//close the stream
stream.close();
getXstudio().createResource( resource );
Debug.print("Created resource: " + resource );
return mapping.findForward( SUCCESS );
}
catch (IOException ioe)
{
request.setAttribute( MESSAGE_KEY, "IOException:" + ioe.getMessage() );
return mapping.findForward( ERROR );
}
.................................
............................
Page resPage = getXstudio().getResByType( "%", ( Integer.parseInt( pageIndex ) - 1 ) * PAGE_LIST_COUNT , PAGE_LIST_COUNT );
String nextPage = null;
String prevPage = null;
String prevLink = null;
String nextLink = null;
if( resPage.isPreviousPageAvailable() )
{
prevPage = Integer.toString( Integer.parseInt( pageIndex ) - 1 );
prevLink = "<a href=resourceList.jsp?page=" + prevPage + ">";
prevLink = prevLink + getResources( request ).getMessage( "prompt.resource.prevpage" );
prevLink = prevLink + "</a>";
}
else
{
prevLink = getResources( request ).getMessage( "prompt.resource.prevpage" );
}
if( resPage.isNextPageAvailable() )
{
nextPage = Integer.toString( Integer.parseInt( pageIndex ) + 1 );
nextLink = "<a href=resourceList.jsp?page=" + nextPage + ">";
nextLink = nextLink + getResources( request ).getMessage( "prompt.resource.nextpage" );
nextLink = nextLink + "</a>";
}
else
{
nextLink = getResources( request ).getMessage( "prompt.resource.nextpage" );
}
Iterator resList = resPage.getList().iterator();
request.setAttribute( RESOURCE_LIST_KEY, resList );
request.setAttribute( PREV_PAGE, prevLink );
request.setAttribute( NEXT_PAGE, nextLink );
return mapping.findForward( SUCCESS );
...........................................下面是根据前面的list生成列表页面的jsp文件
<%@ page import="server.domain.resource.model.ResourceValue" %><%@ taglib uri="struts-html" prefix="html" %>
<%@ taglib uri="struts-bean" prefix="bean" %><html>
<head>
<title><bean:message key="resourcelist.title"/></title>
<link href="<%= request.getContextPath() %>/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<table border="0" width="100%">
<tr>
<td><bean:write name="prevPage" /></td>
<td><bean:write name="nextPage" /></td>
<td></td>
<td></td>
<td></td>
<td>
<html:link page="/upload.jsp">
<bean:message key="prompt.resource.upload" />
</html:link>
</td>
</tr>
<tr>
<td><bean:message key="prompt.resource.id" /></td>
<td><bean:message key="prompt.resource.title" /></td>
<td><bean:message key="prompt.resource.author" /></td>
<td><bean:message key="prompt.resource.time" /></td>
<td><bean:message key="prompt.resource.size" /></td>
<td><bean:message key="prompt.resource.download" /></td>
</tr>
<!-- name = BaseAction.RESOURCE_LIST_KEY -->
<%
Iterator resList = (Iterator)request.getAttribute( "resourceList" );
while( resList.hasNext() )
{
ResourceValue res = (ResourceValue)resList.next();
out.println( "<tr>" );
out.println( "<td>" + res.getResourceId() + "</td>" );
if( res.getContentType().startsWith( "text" ) )
{
out.println( "<td><a href=articleDetail.jspa?id=" + res.getResourceId() + ">" + res.getTitle() + "</a>");
}
else
{
out.println( "<td>" + res.getTitle() + "</td>" );
}
out.println( "<td>" + res.getAuthor() + "</td>" );
out.println( "<td>" + res.getDt() + "</td>" );
out.println( "<td>" + res.getSize() + "</td>" );
out.println( "<td><a href=download.jspa?id=" + res.getResourceId() + ">Download</a></td>" );
out.println( "</tr>" );
}
%>
</table>
</body></html>
...................................
String id = request.getParameter("id");
if( id == null )
{
return mapping.findForward( ERROR );
} try
{
ResourceValue res = getXstudio().getResource( id );
response.setContentType( res.getContentType() );
response.setHeader( "Content-Disposition","attachment; filename=" + res.getTitle() );
byte[] content = res.getContent();
BufferedOutputStream out = new BufferedOutputStream( response.getOutputStream());
out.write( content, 0, content.length );
out.close();
return mapping.findForward( SUCCESS );
}
catch( Exception e )
{
request.setAttribute( MESSAGE_KEY, e.getMessage() );
return mapping.findForward( ERROR );
}...................................最后,上传的jsp文件
<%@ taglib uri="struts-html" prefix="html" %>
<%@ taglib uri="struts-bean" prefix="bean" %>
<html>
<head>
<title><bean:message key="upload.title"/></title>
<link href="style.css" rel="stylesheet" type="text/css">
</head><body>
<html:form action="/resource/create.jspa" method="post" enctype="multipart/form-data">
<table width="75%" border="1">
<tr>
<td>Title(Only for text/html file)</td>
<td><html:text property="title" /></td>
<td> </td>
</tr>
<tr>
<td>File Description/Summary</td>
<td><html:text property="description" /></td>
<td> </td>
</tr>
<tr>
<td>Author</td>
<td><html:text property="author" /></td>
<td> </td>
</tr>
<tr>
<td>File path</td>
<td><html:file property="content" /></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><html:submit /> <html:reset /></td>
<td> </td>
</tr>
</table>
</html:form>
</body>
</html>