使用CXF REST/JSON WebService实现UploadFile接口. 网上搜了一堆(英文),主要是讲用Miltipart的上传,而这个主要是给WebSite使用的,对于WebService,研究了很长时间,下面这个是可以走通的。这里只把实现列出来,原理以后再完善。
- REST服务端
- 接口定义
@Path("/media")
@Produces("application/json")
public interface mediaRest {
@POST
@Path("/UploadFile")
@Produces("application/json")
@Consumes("application/json")
UploadInfoResponse UploadFile(FileUpload file);
}
- 接口实现
@Override
public UploadInfoResponse UploadFile(FileUpload file) {
UploadInfoResponse res = new UploadInfoResponse();
DataHandler handler = file.getDataHandler();
try {
InputStream is = handler.getInputStream();
OutputStream os = new FileOutputStream(new File("D:/uploads/"
+ file.getFilename()));
byte[] b = new byte[100000];
int bytesRead = 0;
while ((bytesRead = is.read(b)) != -1) {
os.write(b, 0, bytesRead);
}
os.flush();
os.close();
is.close();
res.setResponseCode(ResponseCode.SUCCESS);
res.setResponseMessage("Success");
Upload ui = new Upload();
ui.setTotalSize(file.getTotalSize());
ui.setMediaId(file.getMediaId());
ui.setStatus(MediaStatus.eStatus.done.ordinal());
ui.setExpireDate(Util.GetDate(new Date(), Calendar.DAY_OF_MONTH, 90));
res.setUploadInfo(ui);
} catch (IOException e) {
res.setResponseCode(ResponseCode.ERROR_INTERNAL_ERROR);
res.setResponseMessage(e.getMessage());
//e.printStackTrace();
}
return res;
}
- POJO定义:
@XmlRootElement(name = "FileUpload")
public class FileUpload {
private int totalSize;
private int uploadPos;
private int userId;
private int mediaId;
private String filename;
private DataHandler dataHandler;
public DataHandler getDataHandler() {
return dataHandler;
}
public void setDataHandler(DataHandler dataHandler) {
this.dataHandler = dataHandler;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public int getUploadPos() {
return uploadPos;
}
public void setUploadPos(int uploadPos) {
this.uploadPos = uploadPos;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getMediaId() {
return mediaId;
}
public void setMediaId(int mediaId) {
this.mediaId = mediaId;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
}
2. 客户端
public class mydemo {
static WebClient client;
public static void main(String[] args) {
client = WebClient.create("http://localhost:8080/media/services/rest/");
client.header("Content-Type", "application/json");
post();
}
public static void post()
{
String errMsg;
try
{
FileUpload fu = new FileUpload();
FileDataSource ds = new FileDataSource("D:\\assets\\2.vs");
fu.setFilename(ds.getFile().getName());
fu.setMediaId(2);
fu.setUploadPos(0);
fu.setUserId(3);
fu.setTotalSize(ds.getInputStream().available());
fu.setDataHandler(new DataHandler(ds));
//client.post(fu);
client.path("media/UploadFile").accept("application/json").post(fu);
}
catch(Exception ex)
{
errMsg = ex.getMessage();
}
MediaQueryOption query = new MediaQueryOption();
query.setStartPos(0);
query.setEndPos(100);
client.path("media/MyMedias").post(query);
}
}