commons-beanutils.jar
commons-codec-1.3.jar
commons-httpclient-3.1.jar
commons-io-1.3.1.jar
commons-logging-1.1.jar
easymock-2.3.jar
hamcrest-all-1.1.jar
junit-4.4.jar
log4j-1.2.14.jar
svenson-1.2.8.jar
引入这些jar包到项目里面去,然后开始动手写增删改查。。。
过程出了一些问题,因为过程无法重现,只能把evernote里面写的东西贴上了。
在attachment中需要添加一个digest属性,所以自己重新下了源码打成了jar包
在attachment中的getData方法是无实际意义的,使用了也获取不到数据。
路径如果为:
file:/F:/erp workspace/erp/WebRoot/WEB-INF/cfg/content-type.properties获取不到配置文件,
// Properties properties = Resources.getResourceAsProperties(getConfigFilePath());
改用
Properties properties = Resources. getUrlAsProperties( getConfigFilePath());
解决
DesinDocument在createDocument的时候出错,error code 401,没有足够的权限。
改为使用 BaseDocument,正常
附上自己测试类代码,仅供借鉴,无法保证程序的性能:
package com.erp.util;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.ibatis.io.Resources;
import org.apache.log4j.Logger;
import org.jcouchdb.db.Database;
import org.jcouchdb.document.BaseDocument;
import org.jcouchdb.document.Document;
import org.jcouchdb.document.ValueRow;
import org.jcouchdb.document.ViewResult;
/**
* @author Liu Jia
* @version 1.0.0
*/
public class CouchTest {
protected static Logger log = Logger.getLogger(CouchTest.class);
private final static String CONTENT_TYPE_CONFIG_PATH = "cfg/content-type.properties";
private static Database db = new Database("localhost", "couchdb_test");
private static String path ="D:/software/jdk-7-windows-i586.zip";
/**
* 功能: 通过文件对象获取字节数组
*
* @param file 文件对象
* @return 字节数组
*/
public static byte[] getBytesFromFile(File file) {
if (file == null) {
return null;
}
try {
FileInputStream stream = new FileInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream((int) file.length());
byte[] byteArray = new byte[(int) file.length()];
for (int n; (n = stream.read(byteArray)) != -1;) {
out.write(byteArray, 0, n);
}
stream.close();
out.close();
return out.toByteArray();
} catch (IOException e) {
}
return null;
}
/**
* 功能: 把字节数组生成文件
*
* @param byteArray 字节数组
* @param outputFile 输出文件名称,包括后缀名
* @return 文件对象
*/
public static File getFileFromBytes(byte[] byteArray, String outputFile) {
BufferedOutputStream stream = null;
File file = null;
try {
file = new File(outputFile);
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(byteArray);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return file;
}
/**
* 功能: 根据docId获取文档
*
* @param docId
* 文档ID
* @return 文档
*/
public static Document getDocument(String docId) {
BaseDocument doc = db.getDocument(BaseDocument.class, docId);
return doc;
}
/**
* 功能: 获取所有的值行,值行可以用于提取文档的ID
*
* @return 值行列表
*/
@SuppressWarnings("rawtypes")
public static List<ValueRow<Map>> getAllValueRow() {
ViewResult<Map> resultList = db.listDocuments(null, null);
return resultList.getRows();
}
/**
* 功能: 创建附件
*
*/
@SuppressWarnings("rawtypes")
public static void createAttachment() {
List<ValueRow<Map>> resultList = getAllValueRow();
List<String> idList = new ArrayList<String>();
for (ValueRow<Map> row : resultList) {
idList.add(row.getId());
}
path = "D:/software/jdk-7-windows-i586.zip";
File file = new File(path);
System.out.println("==================="+file.length());
byte[] data = getBytesFromFile(file);
String contentType = getContentType(file.getName());
for (String docId : idList) {
BaseDocument document = db.getDocument(BaseDocument.class, docId);
db.createAttachment(docId, document.getRevision(), file.getName(), contentType, data);
}
}
/**
* 功能: 根据文档的后缀获取该文档的内容类型
*
* @param suffix 后缀
* @return 内容类型
*/
public static String getContentType(String name) {
String suffix = name.substring(name.indexOf("."));
String contentType = "";
try {
Properties properties = Resources.getUrlAsProperties(getConfigFilePath());
contentType = properties.getProperty(suffix, "text/plain");
} catch (IOException e) {
e.printStackTrace();
}
return contentType;
}
/**
* 功能: 获取配置文件的
*
* @return
*/
public static String getConfigFilePath() {
String path = CouchTest.class.getClassLoader().getResource("").toString();
path = path.replace("classes/", "");
path += CONTENT_TYPE_CONFIG_PATH;
try {
path = URLDecoder.decode(path, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(path);
return path;
}
/**
*
* @param userId
*/
public static void createDocumentByUserId(String userId) {
BaseDocument document = new BaseDocument();
document.setId(userId);
document.setProperty("privilege", "111 010");
db.createDocument(document);
}
public static void main(String[] args) {
createAttachment();
long start_time = System.currentTimeMillis();
byte[] data = db.getAttachment("f3b34e9592ef0650544554de45003859", path);
getFileFromBytes(data, "erp123.zip");
long end_time = System.currentTimeMillis();
System.out.println(end_time-start_time);
// log.warn("===============" + data);
// createDocumentByUserId("1");
}
}