@XmlRootElement(name = "Customer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
@XmlMimeType("application/octet-stream")
private DataHandler myData;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public DataHandler getMyData() {
return myData;
}
public void setMyPData(DataHandler myData) {
this.myData = myData;
}
}
@javax.xml.ws.soap.MTOM
@WebService(name = "MTOMServerService", portName = "MTOMServer")
public class MTOMServer {
static Logger logger = LogManager.getLogger(MTOMServer.class.getName());
private static final int BUFFER_SIZE = 1024*1024*100;
/**
* 删除
* @param path 文件夹路径
* @throws FileNotFoundException
*/
@WebResult
@XmlMimeType("*/*")
public void delete(@WebParam(name = "path") String path) throws FileNotFoundException {
if (path == null || path.isEmpty()){
logger.error(path+"文件路径为空!");
throw new FileNotFoundException("文件路径为空!");
}
File downloadFile = new File(path);
if (!downloadFile.exists()){
logger.error(path+"不存在!");
throw new FileNotFoundException(path + "不存在!");
}
logger.debug("删除:"+path);
(new PowerDelete()).delete(path);
}
/**
* 下载文件
* @param path 文件夹路径
* @throws FileNotFoundException
* @return DataHandler 数据句柄
*/
@WebResult
@XmlMimeType("*/*")
public Customer download(@WebParam(name = "path") String path) throws FileNotFoundException {
if (path == null || path.isEmpty()){
logger.error(path+"文件路径为空!");
throw new FileNotFoundException("文件路径为空!");
}
final File downloadFile = new File(path);
if (!downloadFile.exists()){
logger.error(path+"不存在!");
throw new FileNotFoundException(path + "不存在!");
}
if (!downloadFile.isFile()){
logger.error(path+"不是一个标准文件!");
throw new FileNotFoundException(path + "不是一个标准文件!");
}
logger.debug("下载文件:"+path);
DataHandler dataHandler = new DataHandler(new FileDataSource(downloadFile));
Customer customer = new Customer();
customer.setName(downloadFile.getName());
customer.setMyPData(dataHandler);
return customer;
}
/**
*
* 下载全部文件
* @param path 文件夹路径
* @throws FileNotFoundException
* @return DataHandler[] 数据句柄
*/
@WebResult
@XmlMimeType("*/*")
public Customer[] downloadMulti(@WebParam(name = "path") String path) throws FileNotFoundException {
if (path == null || path.length() <= 0) {
logger.error(path+"文件路径为空!");
throw new FileNotFoundException("文件夹路径为空!");
}
File file = new File(path);
if (!file.exists()){
logger.error(path+"不存在!");
throw new FileNotFoundException(path + "不存在!");
}
if (file.isFile()){
logger.error(path+"是一个标准文件,请更换下载方法!");
throw new FileNotFoundException(path + "是一个标准文件,请更换下载方法!");
}
logger.debug("下载文件夹:"+path);
final File[] files = file.listFiles();
Customer[] handlers = new Customer[files.length];
for (int i = 0, j = files.length; i < j; i++) {
final String fileName = files[i].getName();
Customer customer = new Customer();
DataHandler dataHandler = new DataHandler(new FileDataSource(files[i]));
customer.setName(fileName);
customer.setMyPData(dataHandler);
handlers[i] = customer;
}
return handlers;
}
/**
* 上载
*
* @param fileName 待上载的文件名
* @param dataHandler 数据句柄
* @throws IOException IO异常
*/
public String upload(@WebParam(name = "fileName") String fileName, @XmlMimeType("*/*") @WebParam(name = "dataHandler") DataHandler dataHandler){
logger.debug("上传文件:"+fileName);
File depository = ConfigManager.getFileDepository();
String urlString = depository.getAbsolutePath() + File.separatorChar + fileName;
InputStream in = null;
OutputStream out = null;
try {
if (fileName == null || fileName.isEmpty()){
logger.error("文件名为空!");
throw new FileNotFoundException("文件名为空!");
}
in = dataHandler.getInputStream();
out = new FileOutputStream(urlString);
byte[] buf = new byte[BUFFER_SIZE];
int read;
while ((read = in.read(buf)) != -1) {
out.write(buf, 0, read);
out.flush();
}
} catch (IOException e) {
logger.error(e.getMessage());
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return urlString;
}
}
// 下载全部文件
try {
List<Customer> dataHandlers = mtomPort.downloadMulti("W:\\TEST\\20140117-144703-4DCD");
byte[] buf = new byte[CHUNK_SIZE];
for (int i = 0, j = dataHandlers.size(); i < j; i++) {
Customer handler = dataHandlers.get(i);
String fileName = handler.getName();
fileName = fileName == null || fileName.isEmpty() ? Integer.toString(i) : fileName;
InputStream in = handler.getMyData().getInputStream();
OutputStream out = new FileOutputStream("G:\\"+fileName);
int read;
while ((read = in.read(buf)) != -1) {
out.write(buf, 0, read);
out.flush();
}
in.close();
out.close();
}
} catch (FileNotFoundException_Exception e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}