controller
@Controller
@RequestMapping("/content")
public class ContentController {
@Resource
private ContentService contentService;
@RequestMapping("/query/list")
@ResponseBody
public EUDataGridResult queryContentList(Long categoryId,@RequestParam(defaultValue="1")Integer page,@RequestParam(defaultValue="30")Integer rows){
EUDataGridResult result = null;
try {
result = contentService.getContentList(categoryId, page, rows);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@RequestMapping("/save")
@ResponseBody
public EgoResult saveContent(TbContent content){
try {
contentService.saveContent(content);
} catch (Exception e) {
e.printStackTrace();
return EgoResult.build(500, "insert error");
}
return EgoResult.ok();
}
@RequestMapping("/edit")
@ResponseBody
public EgoResult eidtContent(TbContent content){
try {
contentService.editContent(content);
} catch (Exception e) {
e.printStackTrace();
return EgoResult.build(500, "edit error");
}
return EgoResult.ok();
}
@RequestMapping("/delete")
@ResponseBody
public EgoResult removeContent(long[] ids){
try {
contentService.deleteContents(ids);
} catch (Exception e) {
e.printStackTrace();
return EgoResult.build(500, "delete error");
}
return EgoResult.ok();
}
service
@Service("contentService")
public class ContentServiceImpl implements ContentService {
@Resource
private TbContentMapper contentMapper;
@Override
public EUDataGridResult getContentList(long catId, int page, int rows) throws Exception {
try {
TbContentExample example = new TbContentExample();
Criteria criteria = example.createCriteria();
criteria.andCategoryIdEqualTo(catId);
PageHelper.startPage(page, rows);
List<TbContent> list = contentMapper.selectByExample(example);
PageInfo<TbContent> pageInfo = new PageInfo<>(list);
EUDataGridResult result = new EUDataGridResult();
result.setTotal(pageInfo.getTotal());
result.setRows(list);
return result;
} catch (Exception e) {
e.printStackTrace();
throw (e);
}
}
@Override
public void saveContent(TbContent content) throws Exception {
try {
Date date = new Date();
content.setCreated(date);
content.setUpdated(date);
contentMapper.insert(content);
} catch (Exception e) {
e.printStackTrace();
throw(e);
}
}
@Override
public void editContent(TbContent content) throws Exception {
try {
Date date = new Date();
content.setUpdated(date);
TbContentExample example = new TbContentExample();
Criteria criteria = example.createCriteria();
criteria.andIdEqualTo(content.getId());
contentMapper.updateByExampleSelective(content, example);
} catch (Exception e) {
e.printStackTrace();
throw(e);
}
}
@Override
public void deleteContents(long[] ids) throws Exception {
try {
for(long id : ids){
contentMapper.deleteByPrimaryKey(id);
}
} catch (Exception e) {
e.printStackTrace();
throw(e);
}
}
}
response
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class EgoResult {
private static final ObjectMapper MAPPER = new ObjectMapper();
private Integer status;
private String msg;
private Object data;
public static EgoResult build(Integer status, String msg, Object data) {
return new EgoResult(status, msg, data);
}
public static EgoResult ok(Object data) {
return new EgoResult(data);
}
public static EgoResult ok() {
return new EgoResult(null);
}
public EgoResult() {
}
public static EgoResult build(Integer status, String msg) {
return new EgoResult(status, msg, null);
}
public EgoResult(Integer status, String msg, Object data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public EgoResult(Object data) {
this.status = 200;
this.msg = "OK";
this.data = data;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public static EgoResult formatToPojo(String jsonData, Class<?> clazz) {
try {
if (clazz == null) {
return MAPPER.readValue(jsonData, EgoResult.class);
}
JsonNode jsonNode = MAPPER.readTree(jsonData);
JsonNode data = jsonNode.get("data");
Object obj = null;
if (clazz != null) {
if (data.isObject()) {
obj = MAPPER.readValue(data.traverse(), clazz);
} else if (data.isTextual()) {
obj = MAPPER.readValue(data.asText(), clazz);
}
}
return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
} catch (Exception e) {
return null;
}
}
public static EgoResult format(String json) {
try {
return MAPPER.readValue(json, EgoResult.class);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static EgoResult formatToList(String jsonData, Class<?> clazz) {
try {
JsonNode jsonNode = MAPPER.readTree(jsonData);
JsonNode data = jsonNode.get("data");
Object obj = null;
if (data.isArray() && data.size() > 0) {
obj = MAPPER.readValue(data.traverse(),
MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
}
return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
} catch (Exception e) {
return null;
}
}
}
util
import java.util.Random;
public class IDUtils {
public static String genImageName() {
long millis = System.currentTimeMillis();
Random random = new Random();
int end3 = random.nextInt(999);
String str = millis + String.format("%03d", end3);
return str;
}
public static long genItemId() {
long millis = System.currentTimeMillis();
Random random = new Random();
int end2 = random.nextInt(99);
String str = millis + String.format("%02d", end2);
long id = new Long(str);
return id;
}
public static void main(String[] args) {
for(int i=0;i< 100;i++)
System.out.println(genItemId());
}
}
ftpUtil
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpUtil {
public static boolean uploadFile(String host, int port, String username, String password, String basePath,
String filePath, String filename, InputStream input) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(host, port);
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
if (!ftp.changeWorkingDirectory(basePath+filePath)) {
String[] dirs = filePath.split("/");
String tempPath = basePath;
for (String dir : dirs) {
if (null == dir || "".equals(dir)) continue;
tempPath += "/" + dir;
if (!ftp.changeWorkingDirectory(tempPath)) {
if (!ftp.makeDirectory(tempPath)) {
return result;
} else {
ftp.changeWorkingDirectory(tempPath);
}
}
}
}
ftp.setFileType(FTP.BINARY_FILE_TYPE);
if (!ftp.storeFile(filename, input)) {
return result;
}
input.close();
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
String fileName, String localPath) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(host, port);
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(remotePath);
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
public static void main(String[] args) {
try {
FileInputStream in=new FileInputStream(new File("C:/Users/neverComeTrue/Desktop/aaaabbbb.doc"));
boolean flag = uploadFile("192.168.214.128", 21, "ftpuser", "ftpuser", "/home/ftpuser/moral","/2015/01/21", "test.doc", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}