function check() {
if ($.trim($("#name").val()).length == 0) {
alert(i18n_noBookName);
return false;
}
if ($("#defaultUrl").attr("checked")) {
$("#bookCoverUrl").attr("value", "../bookImg/default/book_default.png");
}
$("#publish").submit();
}
function checkName() {
if ($.trim($("#name").val()).length == 0) {
$("#noBookName").show();
}
if ($.trim($("#name").val()).length != 0) {
$("#noBookName").hide();
}
}
function clickUpload() {
var coverUrl = document.getElementById("coverUrl");
coverUrl.click();
}
function choosePic(file) {
if (file == null || file == "") {
alert(i18n_noFile);
return false;
}
if (file.lastIndexOf(".") == -1) {
alert(i18n_errorPath);
return false;
}
var allImgExt = ".jpg|.jpeg|.gif|.bmp|.png|";
var extName = file.substring(file.lastIndexOf("."));
if (allImgExt.indexOf(extName + "|") == -1) {
alert(i18n_formatError);
return false;
}
}
$(function() {
var allBoxs = $(":checkbox");
allBoxs.click(function() {
allBoxs.removeAttr("checked");
$(this).attr("checked", "checked");
});
});
function sure() {
var file = document.getElementById("coverUrl").value;
if (file == null || file == "") {
alert(i18n_noFile);
return false;
}
if (file.lastIndexOf(".") == -1) {
alert(i18n_errorPath);
return false;
}
var allImgExt = ".jpg|.jpeg|.gif|.bmp|.png|";
var extName = file.substring(file.lastIndexOf("."));
if (allImgExt.indexOf(extName + "|") == -1) {
alert(i18n_formatError);
return false;
}
$("#loading").ajaxStart(function() {
$(this).show();
})// 开始上传文件时显示一个图片
.ajaxComplete(function() {
$(this).hide();
});// 文件上传完成将图片隐藏起来
$.ajaxFileUpload( {
url : '../upload/upload.action',// 用于文件上传的服务器端请求地址
secureuri : false,// 一般设置为false
fileElementId : 'coverUrl',// 文件上传空间的id属性 <input type="file" id="file"
// name="file" />
dataType : 'json',// 返回值类型 一般设置为json
success : function(data, status) // 服务器成功响应处理函数
{
// console.log(data);
var dataObj = eval("(" + data + ")");
// alert(dataObj.path);
if (dataObj.info == "success") {
$("#uploadPic").attr("src", dataObj.path);
$("#bookCoverUrl").attr("value", dataObj.path);
} else if (dataObj.info == "to big") {
alert(i18n_fileTooBig);
} else if (dataObj.info == "upload fail") {
alert(i18n_uploadFail);
}
},
error : function(data, status, e)// 服务器响应失败处理函数
{
alert(e);
}
});
return false;
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="upload" namespace="/upload" extends="json-default">
<action name="upload" class="uploadAction" method="uploadPic">
<result type="json" name="success">
<param name="contentType">
text/html
</param>
<param name="root">result</param>
</result>
<result type="json" name="error">
<param name="contentType">
text/html
</param>
<param name="root">result</param>
</result>
</action>
</package>
</struts>
package com.augmentum.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private File coverUrl;
private String coverUrlFileName;
private String coverUrlFileContentType;
private String result;
public String uploadPic() throws Exception {
Long pathPre = System.currentTimeMillis();
String path = ServletActionContext.getServletContext().getRealPath(
"/bookImg/users");
JSONObject jsonObj = new JSONObject();
try {
File f = this.getCoverUrl();
FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path + "/"
+ "/" + pathPre + this.getCoverUrlFileName());
if (inputStream.available() >= 2097152) {
// if the picture size bigger than 10701096
jsonObj.put("info", "to big");
result = jsonObj.toString();
return ERROR;
}
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
jsonObj.put("info", "upload fail");
result = jsonObj.toString();
return ERROR;
}
jsonObj.put("path", "../bookImg/users/" + "/" + pathPre
+ coverUrlFileName);
jsonObj.put("info", "success");
result = jsonObj.toString();
return SUCCESS;
}
public File getCoverUrl() {
return coverUrl;
}
public void setCoverUrl(File coverUrl) {
this.coverUrl = coverUrl;
}
public String getCoverUrlFileName() {
return coverUrlFileName;
}
public void setCoverUrlFileName(String coverUrlFileName) {
this.coverUrlFileName = coverUrlFileName;
}
public String getCoverUrlFileContentType() {
return coverUrlFileContentType;
}
public void setCoverUrlFileContentType(String coverUrlFileContentType) {
this.coverUrlFileContentType = coverUrlFileContentType;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
int allRow = this.getUserInfo(id).getBooks().size();
int totalPage = PageDTO.countTotalPage(allRow, pageSize);
PageDTO pageDTO = new PageDTO();
pageDTO.setPageSize(pageSize);
pageDTO.setTotalPage(totalPage);
pageDTO.setCurrentPage(currentPage);
pageDTO.setAllRow(allRow);
pageDTO.setNextPage(currentPage + 1);
pageDTO.setPreviousPage(currentPage - 1);
int offset = PageDTO.countOffset(pageDTO.getCurrentPage(), pageSize);
List<Book> books = null;
String hql = "FROM Book b WHERE b.user.id=" + id
+ " ORDER BY uploadTime desc";
try {
books = bookDao.queryForPage(hql, offset, pageSize);
if (null == books) {
throw new UserServiceException("no book");
}
} catch (DataAccessException e) {
e.printStackTrace();
throw new UserServiceException("query error!");
}
pageDTO.setData(books);
pageDTO.init();
return pageDTO;
if ($.trim($("#name").val()).length == 0) {
alert(i18n_noBookName);
return false;
}
if ($("#defaultUrl").attr("checked")) {
$("#bookCoverUrl").attr("value", "../bookImg/default/book_default.png");
}
$("#publish").submit();
}
function checkName() {
if ($.trim($("#name").val()).length == 0) {
$("#noBookName").show();
}
if ($.trim($("#name").val()).length != 0) {
$("#noBookName").hide();
}
}
function clickUpload() {
var coverUrl = document.getElementById("coverUrl");
coverUrl.click();
}
function choosePic(file) {
if (file == null || file == "") {
alert(i18n_noFile);
return false;
}
if (file.lastIndexOf(".") == -1) {
alert(i18n_errorPath);
return false;
}
var allImgExt = ".jpg|.jpeg|.gif|.bmp|.png|";
var extName = file.substring(file.lastIndexOf("."));
if (allImgExt.indexOf(extName + "|") == -1) {
alert(i18n_formatError);
return false;
}
}
$(function() {
var allBoxs = $(":checkbox");
allBoxs.click(function() {
allBoxs.removeAttr("checked");
$(this).attr("checked", "checked");
});
});
function sure() {
var file = document.getElementById("coverUrl").value;
if (file == null || file == "") {
alert(i18n_noFile);
return false;
}
if (file.lastIndexOf(".") == -1) {
alert(i18n_errorPath);
return false;
}
var allImgExt = ".jpg|.jpeg|.gif|.bmp|.png|";
var extName = file.substring(file.lastIndexOf("."));
if (allImgExt.indexOf(extName + "|") == -1) {
alert(i18n_formatError);
return false;
}
$("#loading").ajaxStart(function() {
$(this).show();
})// 开始上传文件时显示一个图片
.ajaxComplete(function() {
$(this).hide();
});// 文件上传完成将图片隐藏起来
$.ajaxFileUpload( {
url : '../upload/upload.action',// 用于文件上传的服务器端请求地址
secureuri : false,// 一般设置为false
fileElementId : 'coverUrl',// 文件上传空间的id属性 <input type="file" id="file"
// name="file" />
dataType : 'json',// 返回值类型 一般设置为json
success : function(data, status) // 服务器成功响应处理函数
{
// console.log(data);
var dataObj = eval("(" + data + ")");
// alert(dataObj.path);
if (dataObj.info == "success") {
$("#uploadPic").attr("src", dataObj.path);
$("#bookCoverUrl").attr("value", dataObj.path);
} else if (dataObj.info == "to big") {
alert(i18n_fileTooBig);
} else if (dataObj.info == "upload fail") {
alert(i18n_uploadFail);
}
},
error : function(data, status, e)// 服务器响应失败处理函数
{
alert(e);
}
});
return false;
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="upload" namespace="/upload" extends="json-default">
<action name="upload" class="uploadAction" method="uploadPic">
<result type="json" name="success">
<param name="contentType">
text/html
</param>
<param name="root">result</param>
</result>
<result type="json" name="error">
<param name="contentType">
text/html
</param>
<param name="root">result</param>
</result>
</action>
</package>
</struts>
package com.augmentum.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private File coverUrl;
private String coverUrlFileName;
private String coverUrlFileContentType;
private String result;
public String uploadPic() throws Exception {
Long pathPre = System.currentTimeMillis();
String path = ServletActionContext.getServletContext().getRealPath(
"/bookImg/users");
JSONObject jsonObj = new JSONObject();
try {
File f = this.getCoverUrl();
FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path + "/"
+ "/" + pathPre + this.getCoverUrlFileName());
if (inputStream.available() >= 2097152) {
// if the picture size bigger than 10701096
jsonObj.put("info", "to big");
result = jsonObj.toString();
return ERROR;
}
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
jsonObj.put("info", "upload fail");
result = jsonObj.toString();
return ERROR;
}
jsonObj.put("path", "../bookImg/users/" + "/" + pathPre
+ coverUrlFileName);
jsonObj.put("info", "success");
result = jsonObj.toString();
return SUCCESS;
}
public File getCoverUrl() {
return coverUrl;
}
public void setCoverUrl(File coverUrl) {
this.coverUrl = coverUrl;
}
public String getCoverUrlFileName() {
return coverUrlFileName;
}
public void setCoverUrlFileName(String coverUrlFileName) {
this.coverUrlFileName = coverUrlFileName;
}
public String getCoverUrlFileContentType() {
return coverUrlFileContentType;
}
public void setCoverUrlFileContentType(String coverUrlFileContentType) {
this.coverUrlFileContentType = coverUrlFileContentType;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
int allRow = this.getUserInfo(id).getBooks().size();
int totalPage = PageDTO.countTotalPage(allRow, pageSize);
PageDTO pageDTO = new PageDTO();
pageDTO.setPageSize(pageSize);
pageDTO.setTotalPage(totalPage);
pageDTO.setCurrentPage(currentPage);
pageDTO.setAllRow(allRow);
pageDTO.setNextPage(currentPage + 1);
pageDTO.setPreviousPage(currentPage - 1);
int offset = PageDTO.countOffset(pageDTO.getCurrentPage(), pageSize);
List<Book> books = null;
String hql = "FROM Book b WHERE b.user.id=" + id
+ " ORDER BY uploadTime desc";
try {
books = bookDao.queryForPage(hql, offset, pageSize);
if (null == books) {
throw new UserServiceException("no book");
}
} catch (DataAccessException e) {
e.printStackTrace();
throw new UserServiceException("query error!");
}
pageDTO.setData(books);
pageDTO.init();
return pageDTO;