我使用Servlet 3.0,PrimeFaces 6.0,WildFly 8.2,Eclipse Neon,Mozilla或Chrome浏览器。尽管遵循以下这些好的链接:
Oracle Tutorial on File Upload
GitHub: getting the original file name example
我仍然无法确定上传文件的实际文件名。我的问题是在下面提到的servlet中调用方法:
String fileNamer = getFileName(filePart);
给我回文件名为NULL,即fileNamer为null。我究竟做错了什么?请帮忙:
1。)这是我的控制器(servlet):
@WebServlet("/fileUpload")
@MultipartConfig
public class ImageUploadServlet extends HttpServlet {
private String getFileName(Part part) {
for (String cd : part.getHeader("content-disposition").split(";")) {
if (cd.trim().startsWith("filename")) {
return cd.substring(cd.indexOf('=') + 1).trim()
.replace("\"", "");
}
}
return null;
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
HttpSession session = request.getSession(false);
Long savedKundeId = (Long) session.getAttribute(NewCustomerBean.SESSION_ATTRIBUTE_CUST_ID);
Part filePart = null;
PrintWriter pw = null;
try {
filePart = request.getPart("uploadImageForNewCustomerformId");
String fileNamer = getFileName(filePart);
// rest of code not shown here
2.。)我的观点(Prime Faces 6.0 facelet):
<h:form id="newCustomerformId">
<!-- rest of code not shown -->
<p:commandButton type="submit" value="Create Customer"
icon="ui-icon-check"
actionListener="#{newCustomerBean.saveNewCustomer}"
update = "@form"
oncomplete="ajaxUploadFile();"/>
</h:form>
<h:form id="uploadImageForNewCustomerformId"
enctype="multipart/form-data">
<div id="dropzone">
<img id="librarypreview" src='' alt='library'
style="width: 280px; height: 160 px;" /> <select name="top5"
id="flist" size="5" onchange="previewFile()">
</select>
<output id="list"> </output>
</div>
<input id="fileInput" type="file" name = "file"></input>
<span id="uploadStatusId"></span>
</h:form>
3.。)我的Java Scipt函数用于ajax上传文件:
function ajaxUploadFile() {
var form = document.getElementById('uploadImageForNewCustomerformId');
if (form == null)
return;
var formData = new FormData(form);
for (var i = 0; i < fileList.length; i ++){
//append a File to the FormData object
formData.append("file", fileList[i], fileList[i].name);
}
var uploadStatusOutput = document.getElementById("uploadStatusId");
var request = new XMLHttpRequest();
request.open("POST", "/javakurs3-biliothek-jsf-mobile/fileUpload");
request.responseType = 'text';
request.onload = function(oEvent) {
if (request.readyState === request.DONE) {
if (request.status === 200) {
if (request.responseText == "OK") {
form.action = "/javakurs3-biliothek-jsf-mobile/pages/customers.jsf";
form.submit();
return;
}
}
uploadStatusOutput.innerHTML = "Error uploading image";
} // request.readyState === request.DONE
}; // function (oEvent)
request.send(formData);
};
答案 0 :(得分:0)
我终于能够解决问题了。正如BalusC正确地说的那样,我不仅使用Java Script预览图像,还使用Java脚本上传它。这引起了混乱,因为PrimeFaces支持图像预览和使用其自定义标签的图像上传,如下所示。
使用此x.x.x.x
的问题在于它有自己的图像提交或上传按钮。但是,我想提交我新输入的客户数据并使用完全一个按钮和按钮点击上传图像。
我的要求的解决方案是我在ImageUploadServlet中使用了以下代码
p:fileUpload
而不是我在问题中提到的代码:
for (Part fPart : request.getParts()){
if (fPart.getName()!=null && fPart.getName().equals("file") && StringUtils.isNotEmpty(fPart.getSubmittedFileName())){
fileNamer = fPart.getSubmittedFileName();
filePart = fPart;
break;
}
}