我正在开发一个需要上传文件的JIRA附加组件。该文件包含JSON格式的规则,用于定义加载项的行为。 我在管理页面中定义了一个multipart-config servlet和一个multipart / form-data表单。但我无法在servlet端使用getPart方法检索任何部分,它似乎到达"空",就像没有定义任何形式一样。
以下是我的一些代码:
上传规则模板(vm):
...
<form id="upload-rules" class="aui" method="POST" enctype="multipart/form-data">
<input type="hidden" id="uploading" name="uploading" value="y">
<input type="hidden" id="test" name="test" value="y">
<div class="field-group">
<label for="file">Rules config file<span class="aui-icon icon-required">(required)</span></label>
<input type="file" id="file" name="file" class="file">
</div>
<div class="field-group">
<input type="submit" value="Upload" class="button" name="upload" id="upload">
<a href="$baseURL/plugins/servlet/upm">Cancel</a>
</div>
</form>
...
RulesServlet.java
...
@Named("RulesServlet")
@MultipartConfig
public class RulesServlet extends HttpServlet
{
...
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
LOG.debug("doGet method called.");
//Only admin users are admitted
if (!isAdminUser(request)) {
redirectToLogin(request, response);
return;
}
AutoEscalateConfig autoEscalateConfig = null;
...stuff for getting configs...
if ("preupload".equals(request.getParameter("op"))) {
Map<String, Object> context = getContextWithCommonParams(autoEscalateConfig);
// Render the template with the issue inside the context
response.setContentType(CONTENT_TYPE);
renderer.render(UPLOAD_TEMPLATE, context, response.getWriter());
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
LOG.debug("doPost method called.");
//Only admin users are admitted
if (!isAdminUser(request)) {
redirectToLogin(request, response);
return;
}
try {
LOG.debug("Updating rules...");
LOG.debug("Request content type: " + request.getContentType());
LOG.debug("Request parameterMap size: " + request.getParameterMap().size());
LOG.debug("Request parts size: " + request.getParts().size());
Part filePart = request.getPart("file"); // Retrieves <input type="file" name="file">
String fileName = Paths.get(getSubmittedFileName(filePart)).getFileName().toString(); // MSIE fix.
InputStream fileContent = filePart.getInputStream();
...stuff to get rules from file...
response.setContentType(CONTENT_TYPE);
renderer.render(UPLOAD_TEMPLATE, context, response.getWriter());
} catch (Exception e) {
LOG.error("Error creating escalate rules.", e);
throw new ServletException(e);
}
}
private static String getSubmittedFileName(Part part) {
for (String cd : part.getHeader("content-disposition").split(";")) {
if (cd.trim().startsWith("filename")) {
String fileName = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
return fileName.substring(fileName.lastIndexOf('/') + 1).substring(fileName.lastIndexOf('\\') + 1); // MSIE fix.
}
}
return null;
}
}
...
Atlassian的-plugin.xml的
...
<servlet key="mim-autoescalate-upload-rules-servlet" i18n-name-key="mim-autoescalate-rules-upload-servlet.name"
class="cat.palma.jira.escalate.ui.RulesServlet" name="MIM Autoescalate Plugin Upload Rules Servlet">
<description key="mim-autoescalate-rules-upload-servlet.description">The rules upload servlet to populate the MIM Autoescalate Plugin rule table</description>
<url-pattern>/mimautoescalateplugin/rules</url-pattern>
</servlet>
...
<web-item key="mim-autoescalate-link-upload-rules" i18n-name-key="mim-autoescalate-link-upload-rules.name"
name="MIM Autoescalate Plugin Setup Rules Link" section="admin_plugins_menu/mim-autoescalate-section" weight="1010">
<description key="mim-autoescalate-link-upload-rules.description">The Setup Page Link for escalate rules of MIM Autoescalate Plugin</description>
<label key="mim-autoescalate-link-upload-rules.label"></label>
<link linkId="mim-autoescalate-link-upload-rules-link">/plugins/servlet/mimautoescalateplugin/rules?op=preupload</link>
</web-item>
...
的pom.xml
...
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
...
<properties>
<jira.version>7.7.1</jira.version>
<amps.version>6.3.15</amps.version>
<plugin.testrunner.version>1.2.3</plugin.testrunner.version>
<atlassian.spring.scanner.version>1.2.13</atlassian.spring.scanner.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<atlassian.template.renderer.version>2.0.0</atlassian.template.renderer.version>
<activeobjects.version>1.5.0</activeobjects.version>
<spring.version>4.2.5.RELEASE</spring.version>
<gson.version>2.3.1</gson.version>
...
</properties>
...
获取文件并按上传按钮后,来自servlet调试的以下消息将显示在日志中:
[INFO] [talledLocalContainer] 2018-05-17 09:00:55,820 DEBUG admin [c.p.j.escalate.ui.RulesServlet] doPost method called.
[INFO] [talledLocalContainer] 2018-05-17 09:00:55,821 DEBUG admin [c.p.j.escalate.ui.RulesServlet] User performing action admin.
[INFO] [talledLocalContainer] 2018-05-17 09:00:55,822 DEBUG admin [c.p.j.escalate.ui.RulesServlet] Updating rules...
[INFO] [talledLocalContainer] 2018-05-17 09:00:55,822 DEBUG admin [c.p.j.escalate.ui.RulesServlet] Request content type: multipart/form-data; boundary=----WebKitFormBoundaryxRnbEsLcgjpzyKWn
[INFO] [talledLocalContainer] 2018-05-17 09:00:55,822 DEBUG admin [c.p.j.escalate.ui.RulesServlet] Request parameterMap size: 1
[INFO] [talledLocalContainer] 2018-05-17 09:00:55,823 DEBUG admin [c.p.j.escalate.ui.RulesServlet] Request parts size: 0
[INFO] [talledLocalContainer] 2018-05-17 09:00:55,823 ERROR admin [c.p.j.escalate.ui.RulesServlet] Error creating escalate rules.
[INFO] [talledLocalContainer] java.lang.NullPointerException
ParameterMap仅包含&#34; op&#34;来自链接的参数(&#34; ... / plugins / servlet / mimautoescalateplugin / rules?op = preupload&#34;)。 request.getParts()。size()为0,因此 request.getPart(&#34; file&#34;)返回 null ,没有上传文件。似乎没有定义任何形式。
有没有人知道发生了什么或错误在哪里?
提前致谢!!