Merge branch 'web_backend_develope' of

https://git.trustie.net/fhx569287825/aggregation-platform into
web_backend_develope

Conflicts:
	src/com/platform/controller/ExcelController.java
web_backend_develope
chenlw 9 years ago
commit 59205f5917

@ -4,7 +4,9 @@
# 驱动程序
jdbc.mysql.driver=com.mysql.jdbc.Driver
# 连接url
jdbc.mysql.url=jdbc:mysql://192.168.0.110:3306/ftpdata?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false
# 用户名
jdbc.mysql.username=root
# 密码
@ -57,6 +59,14 @@ collect-password=oracle
collect-service-name=orcl
gfs_control_ip=192.168.0.110
gfs_control_rootPassWd=root
HttpClientConstant_URL_IP_PORT=http://192.168.0.110:8088/jfinal/
#=============================================================================================================
# 文件上传下载
#=============================================================================================================
file_upload_path=D:\\
file_download_path=D:\\uuu.xls

@ -2,6 +2,8 @@ package com.platform.controller;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -11,11 +13,17 @@ import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@ -28,8 +36,11 @@ import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import com.base.BaseController;
import com.platform.entities.PagerOptions;
import com.platform.entities.PreDataInfo;
import com.platform.entities.ResumableInfo;
import com.platform.entities.ResumableInfoStorage;
import com.platform.service.IPreDataInfoService;
import com.platform.utils.Configs;
import com.platform.utils.HttpUtils;
import com.platform.utils.UtilsHelper;
@Controller
@ -41,46 +52,115 @@ public class ExcelController extends BaseController{
@Resource(name = "preDataInfoService")
private IPreDataInfoService preDataInfoService;
@RequestMapping(value = "/file/upload" , method = RequestMethod.POST)
public void upload(HttpServletRequest request, HttpServletResponse respone){
System.out.println(request.getAttribute("file"));
System.out.println(request.getParameter("file"));
//解析器解析request的上下文
CommonsMultipartResolver multipartResolver =
new CommonsMultipartResolver(request.getSession().getServletContext());
//先判断request中是否包涵multipart类型的数据
if(multipartResolver.isMultipart(request)){
//再将request中的数据转化成multipart类型的数据
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator iter = multiRequest.getFileNames();
while(iter.hasNext()){
//这里的name为fileItem的alias属性值相当于form表单中name
String name=(String)iter.next();
System.out.println("name:"+name);
//根据name值拿取文件
MultipartFile file = multiRequest.getFile(name);
if(file != null){
String fileName = file.getOriginalFilename();
String path = "D:/test/" + fileName;
File localFile = new File(path);
if(!localFile.getParentFile().exists()) {
//如果目标文件所在的目录不存在,则创建父目录
localFile.getParentFile().mkdirs();
System.out.println("parent:"+localFile.getParentFile().getPath());
// 文件上传处理函数
@RequestMapping(value = "/file/upload", method = RequestMethod.POST)
public void upload(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int resumableChunkNumber = HttpUtils.toInt(
request.getParameter("resumableChunkNumber"), -1);
ResumableInfo info = getResumableInfo(request);
String requestMethod = request.getMethod();
// 处理GET请求
if ("GET".equals(requestMethod)) {
if (info.uploadedChunks
.contains(new ResumableInfo.ResumableChunkNumber(
resumableChunkNumber))) {
response.getWriter().print("Uploaded."); // This Chunk has been
// Uploaded.
} else {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
} else if ("POST".equals(requestMethod)) { // 处理POST请求
RandomAccessFile raf = new RandomAccessFile(info.resumableFilePath,
"rw");
// Seek to position
raf.seek((resumableChunkNumber - 1)
* (long) info.resumableChunkSize);
// Save to file
InputStream is = request.getInputStream();
long readed = 0;
long content_length = request.getContentLength();
byte[] bytes = new byte[1024 * 100];
while (readed < content_length) {
int r = is.read(bytes);
if (r < 0) {
break;
}
raf.write(bytes, 0, r);
readed += r;
}
raf.close();
// Mark as uploaded.
info.uploadedChunks.add(new ResumableInfo.ResumableChunkNumber(
resumableChunkNumber));
if (info.checkIfUploadFinished()) { // Check if all chunks uploaded,
// and change filename
ResumableInfoStorage.getInstance().remove(info);
response.getWriter().print("All finished.");
} else {
response.getWriter().print("Upload");
}
//写文件到本地
try {
file.transferTo(localFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} else { // 不处理非GET/POST请求
throw new IllegalStateException("只接受 POST或GET请求");
}
}
// 文件下载处理函数
@RequestMapping(value = "/file/download")
public ResponseEntity<byte[]> download(HttpServletRequest request,
HttpServletResponse response) throws IOException {
File file = new File(Configs.FILE_DOWNLOAD_PATH);
System.out.println(Configs.FILE_DOWNLOAD_PATH);
if (file.exists()) {
HttpHeaders headers = new HttpHeaders();
String fileName = new String(file.getName().getBytes("UTF-8"),
"iso-8859-1");// 为了解决中文名称乱码问题
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(
FileUtils.readFileToByteArray(file), headers,
HttpStatus.CREATED);
} else {
response.setStatus(500);
return null;
}
}
System.out.println("-----");
respone.setStatus(200);
// 文件上传处理函数
private ResumableInfo getResumableInfo(HttpServletRequest request)
throws ServletException {
String base_dir = Configs.FILE_UPLOAD_PATH;
int resumableChunkSize = HttpUtils.toInt(
request.getParameter("resumableChunkSize"), -1);
long resumableTotalSize = HttpUtils.toLong(
request.getParameter("resumableTotalSize"), -1);
String resumableIdentifier = request
.getParameter("resumableIdentifier");
String resumableFilename = request.getParameter("resumableFilename");
String resumableRelativePath = request
.getParameter("resumableRelativePath");
// Here we add a ".temp" to every upload file to indicate NON-FINISHED
new File(base_dir).mkdir();
String resumableFilePath = new File(base_dir, resumableFilename)
.getAbsolutePath() + ".temp";
ResumableInfoStorage storage = ResumableInfoStorage.getInstance();
ResumableInfo info = storage.get(resumableChunkSize,
resumableTotalSize, resumableIdentifier, resumableFilename,
resumableRelativePath, resumableFilePath);
if (!info.vaild()) {
storage.remove(info);
throw new ServletException("Invalid request params.");
}
return info;
}
@ResponseBody

@ -0,0 +1,68 @@
package com.platform.entities;
import java.io.File;
import java.util.HashSet;
import com.platform.utils.HttpUtils;
public class ResumableInfo {
public int resumableChunkSize;
public long resumableTotalSize;
public String resumableIdentifier;
public String resumableFilename;
public String resumableRelativePath;
public static class ResumableChunkNumber {
public ResumableChunkNumber(int number) {
this.number = number;
}
public int number;
@Override
public boolean equals(Object obj) {
return obj instanceof ResumableChunkNumber ? ((ResumableChunkNumber) obj).number == this.number
: false;
}
@Override
public int hashCode() {
return number;
}
}
// Chunks uploaded
public HashSet<ResumableChunkNumber> uploadedChunks = new HashSet<ResumableChunkNumber>();
public String resumableFilePath;
public boolean vaild() {
if (resumableChunkSize < 0 || resumableTotalSize < 0
|| HttpUtils.isEmpty(resumableIdentifier)
|| HttpUtils.isEmpty(resumableFilename)
|| HttpUtils.isEmpty(resumableRelativePath)) {
return false;
} else {
return true;
}
}
public boolean checkIfUploadFinished() {
// check if upload finished
int count = (int) Math.ceil(((double) resumableTotalSize)
/ ((double) resumableChunkSize));
for (int i = 1; i < count; i++) {
if (!uploadedChunks.contains(new ResumableChunkNumber(i))) {
return false;
}
}
// Upload finished, change filename.
File file = new File(resumableFilePath);
String new_path = file.getAbsolutePath().substring(0,
file.getAbsolutePath().length() - ".temp".length());
file.renameTo(new File(new_path));
return true;
}
}

@ -0,0 +1,64 @@
package com.platform.entities;
import java.util.HashMap;
public class ResumableInfoStorage {
// Single instance
private ResumableInfoStorage() {
}
private static ResumableInfoStorage sInstance;
public static synchronized ResumableInfoStorage getInstance() {
if (sInstance == null) {
sInstance = new ResumableInfoStorage();
}
return sInstance;
}
// resumableIdentifier -- ResumableInfo
private HashMap<String, ResumableInfo> mMap = new HashMap<String, ResumableInfo>();
/**
* Get ResumableInfo from mMap or Create a new one.
*
* @param resumableChunkSize
* @param resumableTotalSize
* @param resumableIdentifier
* @param resumableFilename
* @param resumableRelativePath
* @param resumableFilePath
* @return
*/
public synchronized ResumableInfo get(int resumableChunkSize,
long resumableTotalSize, String resumableIdentifier,
String resumableFilename, String resumableRelativePath,
String resumableFilePath) {
ResumableInfo info = mMap.get(resumableIdentifier);
if (info == null) {
info = new ResumableInfo();
info.resumableChunkSize = resumableChunkSize;
info.resumableTotalSize = resumableTotalSize;
info.resumableIdentifier = resumableIdentifier;
info.resumableFilename = resumableFilename;
info.resumableRelativePath = resumableRelativePath;
info.resumableFilePath = resumableFilePath;
mMap.put(resumableIdentifier, info);
}
return info;
}
/**
* ɾ³ýResumableInfo
*
* @param info
*/
public void remove(ResumableInfo info) {
mMap.remove(info.resumableIdentifier);
}
}

@ -49,4 +49,8 @@ public class Configs {
public static String GATHER_TABLESPACE_PATH=""; //表空间路径
public static String GATHER_TABLE_PASSWORD="1"; //登入密码
public static String FILE_UPLOAD_PATH="";
public static String FILE_DOWNLOAD_PATH="";
}

@ -66,6 +66,10 @@ public class ConfigsLoader implements ServletContextListener {
Configs.GATHER_TABLE_PASSWORD=properties.getProperty("gather-table-user-password");
Configs.FILE_UPLOAD_PATH = properties.getProperty("file_upload_path");
Configs.FILE_DOWNLOAD_PATH = properties.getProperty("file_download_path");
HttpClientConstant.URL_IP_PORT = properties.getProperty("HttpClientConstant_URL_IP_PORT").trim();
Constant.hostIp=properties.getProperty("gfs_control_ip").trim();

@ -0,0 +1,44 @@
package com.platform.utils;
public class HttpUtils {
public static boolean isEmpty(String value) {
return value == null || "".equals(value);
}
/**
* Convert String to long
* @param value
* @param def default value
* @return
*/
public static long toLong(String value, long def) {
if (isEmpty(value)) {
return def;
}
try {
return Long.valueOf(value);
} catch (NumberFormatException e) {
e.printStackTrace();
return def;
}
}
/**
* Convert String to int
* @param value
* @param def default value
* @return
*/
public static int toInt(String value, int def) {
if (isEmpty(value)) {
return def;
}
try {
return Integer.valueOf(value);
} catch (NumberFormatException e) {
e.printStackTrace();
return def;
}
}
}
Loading…
Cancel
Save