Not getting html response from servelet through ajax - java

I have made ckeditor plugin to convert doc file to html, the file is uploaded via servlet witha ajax and converted to html. But I am not getting desired html response from servlet. I am getting the same jsp page in result.
Here is Servlet code.
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// HttpSession
response.setContentType("text/html"); // session;
if (!ServletFileUpload.isMultipartContent(request)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN,
"only multipart requests are allowed");
return;
}
WebappContext webappContext = WebappContext.get(getServletContext());
ServletFileUpload fileUpload = webappContext.getFileUpload();
OfficeDocumentConverter converter = webappContext
.getDocumentConverter();
String outputExtension = FilenameUtils.getExtension(request
.getRequestURI());
FileItem uploadedFile;
try {
uploadedFile = getUploadedFile(fileUpload, request);
// FileHandler.checkForValidExtn(uploadedFile);
} catch (FileUploadException fileUploadException) {
throw new ServletException(fileUploadException);
}
if (uploadedFile == null) {
throw new NullPointerException("uploaded file is null");
}
String inputExtension = FilenameUtils.getExtension(uploadedFile
.getName());
String baseName = FilenameUtils.getBaseName(uploadedFile.getName());
// File fileFolder=new File(getServletContext().getRealPath("/")
// +"images");
// fileFolder.mkdir();
// File inputFile=new File(fileFolder+"\\"+ baseName + "." +
// inputExtension);
File inputFile = new File(getServletContext().getRealPath("/")
+ baseName + "." + inputExtension);
writeUploadedFile(uploadedFile, inputFile);
File outputFile = new File(getServletContext().getRealPath("/")
+ baseName + "." + "html");
// File outputFile=new File(fileFolder+"\\"+ baseName + "." + "html");
// outputFile.mkdirs();
System.out.println("input file path:" + inputFile.getAbsolutePath());
System.out.println("outPut file path:" + outputFile.getAbsolutePath());
try {
// DocumentFormat outputFormat = converter.getFormatRegistry()
// .getFormatByExtension(outputExtension);
long startTime = System.currentTimeMillis();
converter.convert(inputFile, outputFile);
long conversionTime = System.currentTimeMillis() - startTime;
logger.info(String.format(
"successful conversion: %s [%db] to %s in %dms",
inputExtension, inputFile.length(), outputExtension,
conversionTime));
} catch (Exception exception) {
logger.severe(String.format(
"failed conversion: %s [%db] to %s; %s; input file: %s",
inputExtension, inputFile.length(), outputExtension,
exception, inputFile.getName()));
throw new ServletException("conversion failed", exception);
} finally {
inputFile.delete();
}
PrintWriter out = response.getWriter();
out.println("<b>File Sent to server</b>");
out.close();
}
Here is code javascript code for sendi file to server.
CKEDITOR.dialog.add( 'uploadDoc', function( editor )
{ var dialougeDefination =
{ title: editor.lang.uploadDoc.labelName,
resizable: CKEDITOR.DIALOG_RESIZE_BOTH,
minWidth: 250,
minHeight: 50,
onOk: function()
{ if(document.getElementById("flFileBrowser").value == "")
{ alert("No file selected");
//return;
}
else
{
$.ajax({
beforeSend: function(){
{ $("#frmFileUpload").ajaxSubmit();
$.blockUI();
}
},
success: function(result)
{ alert(result);
$.unblockUI();
}
});

The response is actually in the responseText field, so your code should be
alert(result.responseText);

Related

Text file is not downloading in spring mvc 3

I am able to create the file but not able to download it. I am trying to download the file by an ajax call. I am able to receive the file name and file content and successfully able to write the content in a file in application directory but I'm not able to download that file. My code is given bellow :-
The Ajax Code -:
downloadButton.onclick = function() {
//alert(payLoadID);
$.ajax({
type: "POST",
url: "payloadAjax",
data: { payloadData: document.getElementById("modal_paragraph").innerHTML, id: payLoadID },
success : function(data) {
alert("SUCCESS: " +data);
// modal.style.display = "none";
// $("body").css("overflow","auto");
},
error : function(e) {
alert("ERROR: "+ e);
},
done : function(e) {
alert("DONE");
}
})
}
Here is the java code -:
#RequestMapping(value = "/payloadAjax", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)//, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public #ResponseBody
void download(#RequestParam("payloadData") String payloadData, #RequestParam("id") String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("id : " +fileName);
System.out.println("payloadData : " +payloadData);
/* File creation code is here. */
String FOLDER = "/downloadDir";
String FILE_NAME = fileName +".txt";
String appPath = request.getRealPath("");
System.out.println("appPath = " + appPath);
File downloadDirectory = new File(appPath + FOLDER);
if(!downloadDirectory.exists() || !downloadDirectory.isDirectory()) {
downloadDirectory.mkdir();
System.out.println("Directory Created");
} else {
System.out.println("Directory already exists.");
}
File uploadFile = new File(downloadDirectory, FILE_NAME);
if(uploadFile.exists()) {
uploadFile.delete();
System.out.println(uploadFile.getName() +" Has been deleted.");
}
try {
uploadFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("New File successfully created.");
try
{
PrintWriter writer = new PrintWriter(uploadFile, "UTF-8");
writer.println(payloadData);
writer.flush();
writer.close();
System.out.println("File successfully written.");
}
catch(Exception ex) {
ex.printStackTrace();
}
if (uploadFile.exists()) {
System.out.println("Upload file exists. Ready to download....");
} else {
System.out.println("Sorry File not found!!!!");
}
/*File creation code successfully completed.*/
/*File download code starts here */
/*HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_HTML);
headers.setContentLength(uploadFile.length());
headers.setContentDispositionFormData("attachment", "Soubhab_Pathak.txt");
InputStreamResource isr = new InputStreamResource(new FileInputStream(uploadFile));
return new ResponseEntity<InputStreamResource>(isr, headers, HttpStatus.OK);*/
if(response == null) {
System.out.println("Response is null.");
} else {
System.out.println("Response is not null.");
}
System.out.println("111");
OutputStream out = response.getOutputStream();
System.out.println("112");
FileInputStream in = new FileInputStream(uploadFile);
System.out.println("113");
response.setContentType(MediaType.TEXT_PLAIN_VALUE);
response.setHeader("Content-Disposition", "attachment; filename=" + FILE_NAME);
response.setHeader("Content-Length", uploadFile.length() +"");
FileCopyUtils.copy(in, out);
System.out.println("114");
response.flushBuffer();
System.out.println("115");
/*File download code completes here.*/
}
The log is as follows
Any idea?
Finally I am able to solve the problem. This also an example of how to download a string as file in Spring MVC using ajax. Solution is as follows -:
1) one controller action to receive the string from ajax call and store that string in a file in the application server.
2) another controller action where you need to write file download code and this action will receive a GET request.
3) in the ajax call in the success part just call the second action method to download the file.
Now the code is working fine.

Null pointer exception while trying to upload a CSV file to the server

I am trying to upload a csv file to the server. Below is my code in html:
<form method="post" id="uploadCSV" enctype="multipart/form-data">
File to upload: <input type="file" id="uploadfile" name="file" accept="text/csv">
<input type="submit" value="Upload" id="uploadcsv" ng-click="uploadCSV()"> Press here to upload the file!
</form>
And my JS:-
$scope.uploadCSV = function()
{
var fileToLoad = document.getElementById("uploadfile").files[0];
var csvreporturl = "/api/oel/csv";
$http.post(csvreporturl, fileToLoad).then(function(response, status) {
console.log("posted:",response);
});
}
Finally the controller in Spring Boot:-
#RequestMapping(value = "/api/oel/csv", method = RequestMethod.POST)
String uploadFileHandler(#RequestBody MultipartFile fileToLoad) throws FileNotFoundException, IOException
{
String name = fileToLoad.getName();
if (!fileToLoad.isEmpty()) {
try {
byte[] bytes = fileToLoad.getBytes();
// Creating the directory to store file
//String rootPath = System.getProperty("catalina.home");
File dir = new File("../dashboard-0.0.12/oel");
if (!dir.exists())
dir.mkdirs();
// Create the file on server
File serverFile = new File(dir.getAbsolutePath()
+ File.separator + name);
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
return "You successfully uploaded file=" + name;
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name
+ " because the file was empty.";
}
}
I am facing below error:-
Failed to load resource: the server responded with a status of 500
(HTTP/1.1 500)
Possibly unhandled rejection:
{"data":{"timestamp":1510643953084,"status":500,"error":"Internal
Server
Error","exception":"java.lang.NullPointerException","message":"No
message
available","path":"/api/oel/csv"},"status":500,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/api/oel/csv","data":{},"headers":{"Accept":"application/json,
text/plain,
/","Content-Type":"application/json;charset=utf-8"}},"statusText":"HTTP/1.1
500"}
Could someone please help?
Thanks
I would recommend this : Apache Commons FileUpload
if (ServletFileUpload.isMultipartContent(request)) {
FileItemFactory factoryItem = new DiskFileItemFactory();
ServletFileUpload uploadFile = new ServletFileUpload(factoryItem);
try {
List items = uploadFile.parseRequest(request);
Iterator iterator = items.iterator();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
String root = getServletContext().getRealPath("/");
File path = new File(root + "/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
System.out.println(uploadedFile.getAbsolutePath());
item.write(uploadedFile);
}
}
} catch (FileUploadException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}

Uploading an Image to a restful webservice produces an unviewable image

I'm using the following solution to try and receive an image in a restful webservice written in java:
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Produces(MediaType.TEXT_PLAIN)
public String getFile(#FormDataParam("pic") InputStream file,
#QueryParam("uid") String uid) {
try {
storeFile(file, uid);
} catch (IOException ex) {
Logger.getLogger(UploadImage.class.getName()).log(Level.SEVERE, null, ex);
return "failed";
}
return "success";
}
private void storeFile(InputStream input, String uid) throws IOException {
String absPath = PATH_TO_FILES + uid + ".jpg";
try {
OutputStream out = new FileOutputStream(new File(absPath));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(absPath));
while ((read = input.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Here is the client code (javascript):
$scope.fileSelect = function (files) {
var file = files[0];
console.log("File loaded");
console.log(files);
console.log('uid = ' + $scope.uid + ' user = ' + $scope.user);
var formData = new FormData();
formData.append('pic', file);
var requestBody = {"token": $scope.token};
var req = {
method: 'POST',
url: 'http://192.168.0.9/resources/UploadPicture?uid=' + $scope.uid,
headers: {
'Content-Type': undefined
},
data: formData
};
console.log(FormData);
$http(req).then(function(response){
console.log(response);
}, function(error){
console.log(error);
});
};
This code produces a file that isnt viewable. The files im expecting are images.
So i got 2 questions:
Whenever the webservice gets called an a response is return, it seems like the image isnt fully flushed to the harddisk. After a while i can edit it. Is there a way to respond back to the client when the image is actually flushed to the disk?
How can i get the input stream to produce a viewable image when its written to the disk?
--edit--
After some fiddling with the file, i realized if i edit the image in notepad++ and took off the beggining and ending tags for the form boundaries, the image is viewable again:
Produced File
Is there a way for the form boundaries to stop interfering with the image data?
I found a solution using apache commons fileupload:
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Produces(MediaType.TEXT_PLAIN)
public String getFile(#Context HttpServletRequest request, #QueryParam("uid") String uid) {
ServletFileUpload upload = new ServletFileUpload();
try {
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
System.out.println("Form field " + name + " with value "
+ Streams.asString(stream) + " detected.");
} else {
System.out.println("File field " + name + " with file name "
+ item.getName() + " detected.");
// Process the input stream
storeFile(stream, uid);
}
}
return "success";
} catch (FileUploadException ex) {
Logger.getLogger(UploadImage.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(UploadImage.class.getName()).log(Level.SEVERE, null, ex);
}
return "failed.";
}

download file using angular2 and spring

I am trying to download file using spring code and my code are running but there is no file downloaded in download folder. here my code are:-
#RequestMapping(value="/downloadRecordFile/{id}",
method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
#Timed
public void downloadRecordFile(#PathVariable("id") Long id,HttpServletRequest request,HttpServletResponse response) throws IOException {
File fileName = new File("C:\workarea\UplodedRecords\test_annotation.pdf");
if (fileName != null) {
if (!fileName.exists()) {
String errorMessage = "Sorry. The file you are looking for does not exist";
log.info(errorMessage);
OutputStream outputStream = response.getOutputStream();
outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8")));
outputStream.close();
return;
}
String mimeType = URLConnection.guessContentTypeFromName(fileName.getName());
if (mimeType == null) {
mimeType = "application/octet-stream";
}
log.info("mimetype : " + mimeType);
response.setContentType(mimeType);
response.setHeader("Content-Disposition", String.format("inline; filename=\"" + fileName.getName() + "\""));
response.setContentLength((int) fileName.length());
InputStream inputStream = new BufferedInputStream(new FileInputStream(fileName));
//Copy bytes from source to destination(outputstream in this example), closes both streams.
FileCopyUtils.copy(inputStream, response.getOutputStream());
inputStream.close();
response.getOutputStream().flush();
}
}
}
My code is not givivng error or exception but it was not downloading file.
angular 2 code:-
function downloadRecord() {
alert("Downloading");
var record_id = 1234;
downloadRecordFile.query({id: record_id}, onSuccess);
}
here is my service js
(function() {
'use strict';
angular
.module('testApp')
.factory('downloadRecordFile', downloadRecordFile);
downloadRecordFile.$inject = ['$resource'];
function downloadRecordFile($resource) {
var resourceUrl = 'api/downloadRecordFile/:id';
return $resource(resourceUrl, {}, {
'query': { method: 'GET', isArray: true},
'get': {
method: 'GET',
transformResponse: function (data) {
if (data) {
data = angular.fromJson(data);
}
return data;
}
}
});
}
})();
Is there any error in code please help me.

How to Download the file to desktop in java

I am trying to to download the report generated in server to the local machine for that i have written a servlet and calling the servelt from my jsp. But its not ont invoking my servlet... i have added some S.O.P for testing and i am not getting those s.o.ps in log and and the screen display blank.
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
System.out.println("In RRR DownLoadServlet");
outs = response.getOutputStream();
String action = request.getParameter("action");
String strInfodom = "DMINFO88";
//String filename = request.getParameter("filename");
String filename = "/Report_Income Statement.xls";
if (action.equals("6")) {
Vector<String> vProperties = new Vector<String>();
vProperties.addElement("DOCUMENT_UPLOAD_SAVE");
vProperties.addElement("DOCUMENT_UPLOAD_TEMP");
// Properties prop = SMSServices.getConfiguration(vProperties);
// String strUploadSave = (String) prop.get("DOCUMENT_UPLOAD_SAVE");
// String strUploadTemp = (String) prop.get("DOCUMENT_UPLOAD_TEMP");
String completePath = RRRConstants.RRR_PATH+RRRConstants.OUTPUT;
System.out.println("Report File path :::::::::::::::: "+completePath);
if (completePath != null) {
byte arrByte[] = new byte[1024];
int readBytes = -1;
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=" + completePath.substring(completePath.lastIndexOf("/") + 1));
fin = new FileInputStream(new File(completePath));
BufferedInputStream bout = new BufferedInputStream(fin);
while ((readBytes = bout.read(arrByte)) > 0) {
outs.write(arrByte, 0, readBytes);
}
outs.flush();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (outs != null)
outs.close();
if (fin != null)
fin.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
jsp code :
<%
String infodom = request.getParameter("infodom");
String user = (String) session.getAttribute("gsUsrID");
String strURL = "";
FileHandler objFileHandler = new FileHandler(null);
WebServerInfo objWebServer = objFileHandler.getPrimaryWebServerInfo();
String protocol = objWebServer.getServletProtocol();
try
{
strURL = protocol+ "://" + getServletConfig().getServletContext().getInitParameter("FIC_WEBSERVER_IP") + ":" + getServletConfig().getServletContext().getInitParameter("FIC_WEBSERVER_PORT") + request.getContextPath() + "/RRRDownLoadServlet?infodom="+infodom+"&filename=";
}
catch(Exception e)
{
System.out.println(" [Download.jsp] Exception "+e);
}
%>
function downloareport()
{
alert("downloading");
var filename = "/Report_Income Statement.xls";
downloadExcel(filename);
}
function downloadExcel(excelFile)
{
window.location.href = "<%=strURL%>"+ excelFile+"&mode=excel";
}
The jsp code contains some javascript. Check these points:
In javascript; writing just function body doesn't automatically invoke that, one has to make call to the function somewhere.
Also tags for java-scripts seems missing.
The request from the JSP is get, not post. Use doGet(...){} instead of doPost(...){...} in servlet.

Categories