Confluence api 写一段上传附件的java代码
时间: 2024-04-30 10:17:11 浏览: 173
好的,以下是一个简单的上传附件的java代码示例:
```java
import com.atlassian.confluence.api.ConfluenceApi;
import com.atlassian.confluence.api.model.content.Content;
import com.atlassian.confluence.api.model.content.ContentType;
import com.atlassian.confluence.api.model.content.ContentBody;
import com.atlassian.confluence.api.model.content.ContentCreationRequest;
import com.atlassian.confluence.api.model.content.ContentUpdateRequest;
import com.atlassian.confluence.api.model.content.Space;
import com.atlassian.confluence.api.model.content.SpaceSummary;
import com.atlassian.confluence.api.model.content.SpaceUpdateRequest;
import com.atlassian.confluence.api.model.content.Upload;
import com.atlassian.confluence.api.model.content.id.ContentId;
import com.atlassian.confluence.api.model.content.id.SpaceId;
import com.atlassian.confluence.api.model.exception.ForbiddenException;
import com.atlassian.confluence.api.model.exception.InvalidRequestException;
import com.atlassian.confluence.api.model.exception.NotFoundException;
import com.atlassian.confluence.api.model.exception.PermissionException;
import com.atlassian.confluence.api.model.exception.RateLimitExceededException;
import com.atlassian.confluence.api.model.exception.ServerErrorException;
import com.atlassian.confluence.api.model.exception.UnknownException;
import com.atlassian.confluence.api.model.pagination.Pagination;
import com.atlassian.confluence.api.model.person.Person;
import com.atlassian.confluence.api.model.person.UserDetails;
import com.atlassian.confluence.api.model.person.Username;
import com.atlassian.confluence.api.model.permissions.PermissionCheckResult;
import com.atlassian.confluence.api.service.content.ContentService;
import com.atlassian.confluence.api.service.content.SpaceService;
import com.atlassian.confluence.api.service.search.SearchService;
import com.atlassian.confluence.api.service.user.UserService;
import com.atlassian.fugue.Option;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
public class ConfluenceAttachmentUploader {
private final ConfluenceApi confluence;
private final String pageTitle;
private final String pageContent;
private final String attachmentFilePath;
public ConfluenceAttachmentUploader(ConfluenceApi confluence, String pageTitle, String pageContent, String attachmentFilePath) {
this.confluence = confluence;
this.pageTitle = pageTitle;
this.pageContent = pageContent;
this.attachmentFilePath = attachmentFilePath;
}
public void uploadAttachment() throws IOException, PermissionException {
// Create page content
ContentBody contentBody = ContentBody.builder()
.value(pageContent)
.representation(ContentBody.Representation.STORAGE)
.build();
ContentCreationRequest contentCreationRequest = ContentCreationRequest.builder()
.space(confluence.getContentService().find().byTitle(ContentType.SPACE, "My Space")).get().getId())
.type(ContentType.PAGE)
.title(pageTitle)
.body(contentBody)
.build();
Content page = confluence.getContentService().create(contentCreationRequest).claim();
// Upload attachment
InputStream inputStream = new FileInputStream(new File(attachmentFilePath));
Upload upload = confluence.getContentService().upload(page.getId(), "attachment.png", inputStream).claim();
// Update page with attachment
ContentUpdateRequest contentUpdateRequest = ContentUpdateRequest.builder()
.id(page.getId())
.title(page.getTitle())
.version(page.getVersion())
.body(contentBody)
.metadata(page.getMetadata())
.attachments(Arrays.asList(upload))
.build();
confluence.getContentService().update(contentUpdateRequest).claim();
}
}
```
请注意,您需要替换以下变量:
- `confluence`: 您的ConfluenceApi对象。
- `pageTitle`: 您要创建的页面的标题。
- `pageContent`: 您要创建的页面的内容。
- `attachmentFilePath`:您要上传的附件的文件路径。
这个示例假设您已经有一个名为"My Space"的空间,您需要将其替换为您实际的空间名称。同时,您还需要进行适当的错误处理和认证,以确保您的代码可以正常运行。
阅读全文