您的位置:首页 > 其它

Jira插件开发中的附件上传

2013-11-27 11:39 274 查看
介绍一下附件上传的操作,内容是这样,通过一个自定义Action,上传附件同时根据在Action的动作里面创建一个Issue,然后附件挂到该Issue上去

 

test.vm

Html代码  


<form enctype="multipart/form-data" action="$requestContext.baseUrl/secure/TestAction.jspa" method="post">  
    <input type="file" name="file" >  
    <input type="submit">  
</form>  

 

TestAction.java

Java代码  


@Override  
    protected String doExecute() throws Exception {  
        MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) ServletActionContext.getRequest();  
        File f = wrapper.getFile("file");   //表单中的name  
  
        IssueInputParameters issueInputParameters = new IssueInputParametersImpl();  
        issueInputParameters.setProjectId(10000l).setIssueTypeId("1").setSummary("测试附件").setReporterId("admin").setAssigneeId("admin");  
        IssueService.CreateValidationResult createValidationResult = issueService.validateCreate(this.getLoggedInUser(), issueInputParameters);  
        if (createValidationResult.isValid()) {  
            IssueService.IssueResult createResult = issueService.create(getLoggedInUser(), createValidationResult);  
            MutableIssue mutableIssue = createResult.getIssue();  
            attachmentManager.createAttachment(f, wrapper.getFilesystemName("file"), wrapper.getContentType("file"), getLoggedInUser(), mutableIssue);  
        }  
          
        return SUCCESS;  
    }  

 

对于文件上传的表单需要这样处理

Java代码  


MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) ServletActionContext.getRequest();  

 得到wrapper对象后,

Java代码  


//得到要上传的文件对象  
wrapper.getFile("file");  
//取得文件名称  
wrapper.getFilesystemName("file");  
//取得文件类型  
wrapper.getContentType("file");  

 创建好Issue之后使用AttachmentManager中的createAttachment方法来将附件和Issue关联

Java代码  


ChangeItemBean createAttachment(File file, String filename, String contentType, User author, Issue issue) throws AttachmentException;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息