|
发表于 2011-10-31 13:43:06
|
显示全部楼层
Re:≈strust2
一开始InputStream是通过request.getInputStream()得到的,那么要clear()是new 一個InputStream出來吗?还是其他的什么?
不過我觉得这应该不是重点,因为在不用struts的时候,是可以很好的使用的。。。
之后我在网上查找解决方案时,看到了一个帖子,说是不用getInputStream()方法,用其他的方法從而达到上传的目的。然后我发现他提供的方法俺这里也不好使,他的方法是:
commonUpload.jsp
HTML code
<%@ page language="java" import="java.util.*,org.apache.commons.fileupload.disk.*,java.io.*,
org.apache.commons.fileupload.servlet.*,org.apache.commons.fileupload.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
DiskFileItemFactory factory=new DiskFileItemFactory();
//设置上傳工厂的限制
factory.setSizeThreshold(1024*1024*20);
factory.setRepository(new File(request.getRealPath("/")));
//创建一个上传文件的ServletFileUpload对象
ServletFileUpload upload=new ServletFileUpload(factory);
//设置上传文件最大接收20M
upload.setSizeMax(20*1024*1024);
//處理http请求,items是所有的表单项
List items=upload.parseRequest(request);
//遍历所有的表单项
for(Iterator it=items.iterator();it.hasNext();){
FileItem item=(FileItem)it.next();
if(item.isFormField()){
String name=item.getFieldName();
String value=item.getString("GBK");
out.println("表單域的name=value对为:"+name+"="+value+"<br/>");
}else{
//取得文件域的表单名
String fieldName=item.getFieldName();
//取得文件名
String fileName=item.getName();
//取得文件的类型
String cotentType=item.getContentType();
//以当前时间来生成文件的文件名
FileOutputStream fos=new FileOutputStream(request.getRealPath("/")+System.currentTimeMillis()
+fileName.substring(fileName.lastIndexOf("."),fileName.length()));
//如果上传文件域对应文件的内容已经在内存中
if(item.isInMemory()){
fos.write(item.get());
}else{
//如果文件内容不存在在内存中
//获取上传文件的输入流
InputStream is=item.getInputStream();
byte[] buffer=new byte[1024];
int len;
//读取上传文件的內容,并将其写入服务器的文件中
while((len=is.read(buffer))>0){
fos.write(buffer,0,len);
}
is.close();
fos.close();
}
}
}
%>
用了他的方法最后输出依旧是没有完成上传效果,然后我在
List items=upload.parseRequest(request);
代码后加上了一句out.println(items);
顯示的结果是[]
也就是说在表单提交到页面的过程中还是什么都没取到
我的表单页面application.jsp代码是:
HTML code
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<%
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 'application.jsp' starting page</title>
</head>
<body>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="Login">
上传文件:<input type="file" name="file"/><br/>
请求參数:<input type="text" name="wawa"/><br/>
<input name="dd" type="submit" value="提交"/>
</form>
</body>
</html>
然后struts.xml
XML code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="devMode" value="true"></constant>
<package name="lee" namespace="/" extends="struts-default">
<action name="Login" class="lee.LoginAction">
<result name="success">/commonUpload.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
然后是LoginAction.java
Java code
package lee;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
@Override
public String execute() throws Exception{
return SUCCESS;
}
最后是使用時的log:
2011-10-28 16:07:56,091 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:68 - Entering nullPropertyValue [target=[com.opensymphony.xwork2.DefaultTextProvider@cb9c42], property=struts]
2011-10-28 16:07:56,092 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:68 - Entering nullPropertyValue [target=[com.opensymphony.xwork2.DefaultTextProvider@cb9c42], property=org]
2011-10-28 16:07:59,161 INFO org.apache.struts2.dispatcher.Dispatcher.info:42 - Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
2011-10-28 16:07:59,162 DEBUG org.apache.struts2.dispatcher.Dispatcher.debug:68 - saveDir=D:\\JavaUseTools\\apache-tomcat-7.0.11\\apache-tomcat-7.0.11\\work\\Catalina\\localhost\\struts2_test_0800
2011-10-28 16:07:59,271 DEBUG org.apache.struts2.dispatcher.multipart.MultiPartRequest.debug:68 - Found item file
2011-10-28 16:07:59,271 DEBUG org.apache.struts2.dispatcher.multipart.MultiPartRequest.debug:68 - Item is a file upload
2011-10-28 16:07:59,272 DEBUG org.apache.struts2.dispatcher.multipart.MultiPartRequest.debug:68 - Found item wawa
2011-10-28 16:07:59,273 DEBUG org.apache.struts2.dispatcher.multipart.MultiPartRequest.debug:68 - Item is a normal form field
2011-10-28 16:07:59,273 DEBUG org.apache.struts2.dispatcher.multipart.MultiPartRequest.debug:68 - Found item dd
2011-10-28 16:07:59,274 DEBUG org.apache.struts2.dispatcher.multipart.MultiPartRequest.debug:68 - Item is a normal form field
2011-10-28 16:07:59,275 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:68 - Entering nullPropertyValue [target=[com.opensymphony.xwork2.DefaultTextProvider@cb9c42], property=struts]
2011-10-28 16:07:59,276 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:68 - Entering nullPropertyValue [target=[com.opensymphony.xwork2.DefaultTextProvider@cb9c42], property=struts]
2011-10-28 16:07:59,276 DEBUG com.opensymphony.xwork2.DefaultActionProxy.debug:68 - Creating an DefaultActionProxy for namespace / and action name Login
2011-10-28 16:07:59,277 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - intercept '//Login' {
2011-10-28 16:07:59,279 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - applied invocation context locale=en
2011-10-28 16:07:59,280 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - before Locale=en
2011-10-28 16:07:59,280 DEBUG com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.debug:68 - Setting static parameters {}
2011-10-28 16:07:59,281 DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor.debug:68 - Setting params NONE
2011-10-28 16:07:59,281 DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor.debug:68 - Setting params dd => [ 提交 ] file => [ D:\\JavaUseTools\\apache-tomcat-7.0.11\\apache-tomcat-7.0.11\\work\\Catalina\\localhost\\struts2_test_0800\\upload__7f40b9a_1334992381b__8000_00000000.tmp ] fileContentType => [ text/plain ] fileFileName => [ eclipse快捷鍵.txt ] wawa => [ ]
2011-10-28 16:07:59,288 DEBUG org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.debug:68 - Validating //Login with method execute.
2011-10-28 16:07:59,289 DEBUG com.opensymphony.xwork2.validator.ValidationInterceptor.debug:68 - Invoking validate() on action lee.LoginAction@ebf121
2011-10-28 16:07:59,289 DEBUG com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil.debug:68 - cannot find method [validateExecute] in action [lee.LoginAction@ebf121]
2011-10-28 16:07:59,290 DEBUG com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil.debug:68 - cannot find method [validateDoExecute] in action [lee.LoginAction@ebf121]
2011-10-28 16:07:59,291 DEBUG com.opensymphony.xwork2.DefaultActionInvocation.debug:68 - Executing action method = null
2011-10-28 16:07:59,292 DEBUG org.apache.struts2.dispatcher.ServletDispatcherResult.debug:68 - Forwarding to location /commonUpload.jsp
2011-10-28 16:07:59,292 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:68 - Entering nullPropertyValue [target=[lee.LoginAction@ebf121, com.opensymphony.xwork2.DefaultTextProvider@cb9c42], property=struts]
2011-10-28 16:07:59,298 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:68 - Entering nullPropertyValue [target=[lee.LoginAction@ebf121, com.opensymphony.xwork2.DefaultTextProvider@cb9c42], property=org]
2011-10-28 16:07:59,301 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - after Locale=en
2011-10-28 16:07:59,302 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - intercept }
那位大哥 行行好 帮俺分析一下 ~~~~~~~~實在是郁悶了很久了 |
|