uploadify3.2.1下载地址:http://www.uploadify.com/download/ 点击打开链接
uploadify3.2.1英文文档说明:http://www.uploadify.com/documentation/ 点击打开链接
uploadify3.2.1中文文档说明:http://blog.csdn.net/lithinkingboy/article/details/45539371 点击打开链接
uploadify在线中文文档:http://slabs.sinaapp.com/uploadifydoc/ 点击打开链接
前端HTML代码:
<div id='uploadSoftwareDiv' class="upload_software hide">
<span style="color:#ff0000;"><input id='software' type='file' name='software' style="z-index: 101"></span>
<span style="color:#ff0000;"><div id="fileQueue" style="z-index: 101"></div></span>
<input id='uploadSoftware' class="uploadify-button" style="width: 120px; height: 32px; z-index: 101" type='button'
value='上传' /> <input id='cancleSoftware' class="uploadify-button" style="width: 120px; height: 32px; z-index: 101"
type='button' value='取消' />
</div>
JS代码:
$(function() {
//初始化控件
$('#software').uploadify({
'uploader' : 'softwareAction!uploadSoftware',// 上传文件到服务器的地址
'cancelImg' : getRootPath() + '/js/uploadify/cancel.png',
'fileDataName' : 'software',
'fileObjName' : 'software',
'queueID' : 'fileQueue',
'queueSizeLimit' : 1,
'displayData' : 'percentage',
'method' : 'post',
'folder' : '/data',
'auto' : false,
'buttonText' : '选择文件...',
'onUploadSuccess' : function(file, data, response) {// 单个文件上传成功触发
// data就是action中返回来的数据
},
'onQueueComplete' : function() {// 所有文件上传完成
$('#uploadSoftwareDiv').hide();
mask.hide();
alert("文件上传成功!");
},
'onUploadError' : function(file, errorCode, errorMsg, errorString) {
alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
}
});
$("#uploadSoftware").on('click', function() {
$('#software').uploadify('settings', 'formData', {
'softwareId' : _this.id
});
$('#software').uploadify('upload', '*');//上传
});
function getRootPath() {
// 获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp
var curWwwPath = window.document.location.href;
// 获取主机地址之后的目录,如: uimcardprj/share/meun.jsp
var pathName = window.document.location.pathname;
var pos = curWwwPath.indexOf(pathName);
// 获取主机地址,如: http://localhost:8083
var localhostPaht = curWwwPath.substring(0, pos);
// 获取带"/"的项目名,如:/uimcardprj
var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
return (localhostPaht + projectName);
}
});
如果你要在上传的时候附加一些数据,就可以用$("#uploadSoftware").on('click', function() {
$('#software').uploadify('settings', 'formData', {
'softwareId' : _this.id
});
$('#software').uploadify('upload', '*');//上传</span>
});
之后使用$('#software').uploadify('upload', '*');这样才真正开始上传
后台java处理代码
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class SoftwareAction extends ActionSupport implements ServletResponseAware, ServletRequestAware, SessionAware {
private static final long serialVersionUID = -4887717911648338263L;
private HttpServletRequest request;// request
private HttpServletResponse response;// response
private Map<String, Object> session;
private File software;
private String softwareFileName;//注意这里是softwareFileName,由File software决定
private String filePath = "D:\\upload\\";
public void uploadSoftware() throws IOException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("imp", "导入失败!");
PrintWriter pw = null;
// String path =
// ServletActionContext.getServletContext().getRealPath("/upload");
File file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
try {
if (this.software != null) {
File f = this.getSoftware();
String fileName = UUID.randomUUID().toString();
FileInputStream inputStream = new FileInputStream(f);
String name = fileName + softwareFileName.substring(softwareFileName.lastIndexOf("."));
FileOutputStream outputStream = new FileOutputStream(filePath + name);
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
outputStream.close();
jsonObject.put("imp", "导入成功!");
}
} catch (Exception e) {
e.printStackTrace();
}
response.setContentType("text/html;charset=UTF-8");
pw = response.getWriter();
pw.print(jsonObject);
}
public void downloadSoftware() {
String softwareId = request.getParameter("softwareId");
SoftwareAddress softwareAddress = softwareAddressService.getLatestSoftwareAddress(new Integer(softwareId));
if (null != softwareAddress) {
// String path =
// ServletActionContext.getServletContext().getRealPath("/upload");
String filePath = this.filePath + softwareAddress.getAddress();
try {
File file = new File(filePath);
String ext = softwareAddress.getAddress().substring(softwareAddress.getAddress().lastIndexOf(".") + 1)
.toUpperCase();
Softwares softwares = softwareAddress.getSoftwares();
String fileName = softwares.getSoftwarename() + "_" + softwares.getSoftwaretype() + "_"
+ softwares.getVersion() + "." + ext;
InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.setContentType("text/plain");
response.setHeader("Content-disposition", "attachment;filename="
+ new String(fileName.getBytes(), "ISO8859-1"));
OutputStream outputStream = response.getOutputStream();
outputStream.write(buffer);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Resource
public void setSoftwareService(SoftwareService softwareService) {
this.softwareService = softwareService;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
this.response.setContentType("text/html;charset=UTF-8");
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public File getSoftware() {
return software;
}
public void setSoftware(File software) {
this.software = software;
}
public String getSoftwareFileName() {
return softwareFileName;
}
public void setSoftwareFileName(String softwareFileName) {
this.softwareFileName = softwareFileName;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public void setSession(Map<String, Object> arg0) {
this.session = arg0;
}
}
<input id='software' type='file' name='software' style="z-index: 101"> 和'fileDataName' : 'software', 'fileObjName' : 'software',与private File software;要保持input的id、fileData、fileObjName、File一致!!!
问题:上传小文件时没问题,但是上传稍大文件时uploadify报 IO Error。
原因:可能是tomcat限制了上传文件的大小。
解决方案:在struts.xml中配置 <constant name="struts.multipart.maxSize" value="2000000000" /> value可根据需要配置大小(单位:bit)