我上传文件后,想把上传进度返回browser,现在只是临时做到这里。可就出现了问题:
第一:点击submit,出现文件下载的窗口,而下载的却是uploadFile.action.
第二:myeclipse中console窗口中是如下信息:
0
0
0
0
0
0
2
2
2010-10-14 19:17:06 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Entferne Datei doc emp\upload_4d1af6c8_12baa750c17__8000_00000000.tmp
2010-10-14 19:17:06 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Resource Leaking: Could not remove uploaded file 'E:\Program Files\Apache Software Foundation\Tomcat 6.0\bin\emp\upload_4d1af6c8_12baa750c17__8000_00000000.tmp'.
2010-10-14 19:17:06 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Entferne Datei doc emp\upload_4d1af6c8_12baa750c17__8000_00000000.tmp
2010-10-14 19:17:06 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Resource Leaking: Could not remove uploaded file 'E:\Program Files\Apache Software Foundation\Tomcat 6.0\bin\emp\upload_4d1af6c8_12baa750c17__8000_00000000.tmp'.
40
40
46
46
54
////////////////////////////////////////
struts.xml如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<constant name="struts.devMode" value="true"/>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.multipart.maxSize" value="3305242880"></constant>
<package name="test" extends="json-default">
<action name="uploadFile" class="com.start.UpFile" method="uploadFile">
<result type="chain" name="success">
<param name="actionName">getBacknum</param>
</result>
</action>
<action name="getBacknum" class="com.start.UpFile" method="getBacknum">
<result type="json"></result>
</action>
</package>
</struts>
/////
UpFile.java如下:
package com.start;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.opensymphony.xwork2.*;
import org.apache.commons.fileupload.*;
import org.apache.struts2.ServletActionContext;
import org.apache.commons.logging.*;import org.apache.struts2.ServletActionContext;
import org.apache.struts2.views.annotations.*;
public class UpFile extends ActionSupport implements Runnable{ private File doc;
private String fileName;
private String contentType;
private Map map=new HashMap();;
static int renum=0;
public String uploadFile(){
new Thread(this).start();
return SUCCESS;
}
public String getBacknum(){
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("application/json");
PrintWriter out;
try {
out = response.getWriter();
System.out.println(renum);
out.write("{'"+renum+"'}");
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
public void run() {
File des=new File("c://",fileName);
byte[]buffer=new byte[1024];
try {
InputStream is=new BufferedInputStream(new FileInputStream(doc));
OutputStream os=new BufferedOutputStream(new FileOutputStream(des));
int b=is.read(buffer);
int size=is.available();
int currentSize=0;
currentSize=b;
while(b!=-1){
os.write(buffer);
b=is.read(buffer);
currentSize+=b;
renum=(int)(currentSize*1.0/size*100);
}
is.close();
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public File getDoc() {
return doc;
}
public void setDoc(File doc) {
this.doc = doc;
}
public String geDoctFileName() {
return fileName;
}
public void setDocFileName(String fileName) {
this.fileName = fileName;
}
public String getDcoContentType() {
return contentType;
}
public void setDocContentType(String contentType) {
this.contentType = contentType;
}
public int getRenum() {
return renum;
}
public void setRenum(int renum) {
this.renum = renum;
}
}
index.jsp页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<script type="text/javascript" src="js/jquery-1.4.2.js" ></script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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">
-->
<script type="text/javascript">
function getMessage(){
$.post("getBacknum.action",function(data,status){
setTimeout("getMessage()",1000);
},"string");
}
$(document).ready(function(){
$("#submit").click(function(){
setTimeout("getMessage()",1000);
});
});
</script>
</head>
<s:form action="uploadFile" method="post" enctype="multipart/form-data">
<s:file name="doc"></s:file>
<s:submit id="submit"></s:submit>
</s:form>
<body>
</body>
</html>
第一:点击submit,出现文件下载的窗口,而下载的却是uploadFile.action.
第二:myeclipse中console窗口中是如下信息:
0
0
0
0
0
0
2
2
2010-10-14 19:17:06 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Entferne Datei doc emp\upload_4d1af6c8_12baa750c17__8000_00000000.tmp
2010-10-14 19:17:06 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Resource Leaking: Could not remove uploaded file 'E:\Program Files\Apache Software Foundation\Tomcat 6.0\bin\emp\upload_4d1af6c8_12baa750c17__8000_00000000.tmp'.
2010-10-14 19:17:06 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Entferne Datei doc emp\upload_4d1af6c8_12baa750c17__8000_00000000.tmp
2010-10-14 19:17:06 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Resource Leaking: Could not remove uploaded file 'E:\Program Files\Apache Software Foundation\Tomcat 6.0\bin\emp\upload_4d1af6c8_12baa750c17__8000_00000000.tmp'.
40
40
46
46
54
////////////////////////////////////////
struts.xml如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<constant name="struts.devMode" value="true"/>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.multipart.maxSize" value="3305242880"></constant>
<package name="test" extends="json-default">
<action name="uploadFile" class="com.start.UpFile" method="uploadFile">
<result type="chain" name="success">
<param name="actionName">getBacknum</param>
</result>
</action>
<action name="getBacknum" class="com.start.UpFile" method="getBacknum">
<result type="json"></result>
</action>
</package>
</struts>
/////
UpFile.java如下:
package com.start;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.opensymphony.xwork2.*;
import org.apache.commons.fileupload.*;
import org.apache.struts2.ServletActionContext;
import org.apache.commons.logging.*;import org.apache.struts2.ServletActionContext;
import org.apache.struts2.views.annotations.*;
public class UpFile extends ActionSupport implements Runnable{ private File doc;
private String fileName;
private String contentType;
private Map map=new HashMap();;
static int renum=0;
public String uploadFile(){
new Thread(this).start();
return SUCCESS;
}
public String getBacknum(){
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("application/json");
PrintWriter out;
try {
out = response.getWriter();
System.out.println(renum);
out.write("{'"+renum+"'}");
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
public void run() {
File des=new File("c://",fileName);
byte[]buffer=new byte[1024];
try {
InputStream is=new BufferedInputStream(new FileInputStream(doc));
OutputStream os=new BufferedOutputStream(new FileOutputStream(des));
int b=is.read(buffer);
int size=is.available();
int currentSize=0;
currentSize=b;
while(b!=-1){
os.write(buffer);
b=is.read(buffer);
currentSize+=b;
renum=(int)(currentSize*1.0/size*100);
}
is.close();
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public File getDoc() {
return doc;
}
public void setDoc(File doc) {
this.doc = doc;
}
public String geDoctFileName() {
return fileName;
}
public void setDocFileName(String fileName) {
this.fileName = fileName;
}
public String getDcoContentType() {
return contentType;
}
public void setDocContentType(String contentType) {
this.contentType = contentType;
}
public int getRenum() {
return renum;
}
public void setRenum(int renum) {
this.renum = renum;
}
}
index.jsp页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<script type="text/javascript" src="js/jquery-1.4.2.js" ></script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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">
-->
<script type="text/javascript">
function getMessage(){
$.post("getBacknum.action",function(data,status){
setTimeout("getMessage()",1000);
},"string");
}
$(document).ready(function(){
$("#submit").click(function(){
setTimeout("getMessage()",1000);
});
});
</script>
</head>
<s:form action="uploadFile" method="post" enctype="multipart/form-data">
<s:file name="doc"></s:file>
<s:submit id="submit"></s:submit>
</s:form>
<body>
</body>
</html>
String inputValue = (String) fileParameterNames.nextElement();
File[] files = multiWrapper.getFiles(inputValue);
for (File currentFile : files) {
if (LOG.isInfoEnabled()) {
LOG.info(getTextMessage(action, "struts.messages.removing.file", new Object[]{inputValue, currentFile}, ac.getLocale()));
}
if ((currentFile != null) && currentFile.isFile()) {
if (currentFile.delete() == false) {
LOG.warn("Resource Leaking: Could not remove uploaded file '"+currentFile.getCanonicalPath()+"'.");
}
}
}
}
.getServletContext().getRealPath("/")
+ getUploadFileFileName());
System.out.println("getUploadFileContentType()="
+ getUploadFileContentType());
FileInputStream fis = new FileInputStream(getUploadFile());
byte[] buffer = new byte[10 * 1024 * 1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
return "SUCCESS";
}