I'm trying to create a servlet that is able to unzip a folder which contains 3 csv files and then print out the data of each csv file.
I have been trying to use ZipInputStream but it does not provide me the capability of reading/printing content of each csv.
As i'm building this web app on GAE, I'm unable to use FileOutputStream.
Are there ways to use ZipInputStream to unzip and read individual csv without the need to create a csv on GAE?
public class AdminBootStrap extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
try {
ServletFileUpload upload = new ServletFileUpload();
resp.setContentType("text/plain");
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream in = item.openStream();
if (item.isFormField()) {
out.println("Got a form field: " + item.getFieldName());
} else {
out.println("Got an uploaded file: " + item.getFieldName() +
", name = " + item.getName());
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(in));
ZipEntry entry;
// Read each entry from the ZipInputStream until no
// more entry found indicated by a null return value
// of the getNextEntry() method.
//
while ((entry = zis.getNextEntry()) != null) {
out.println("Unzipping: " + entry.getName());
//until this point, i'm only available to print each csv name.
//What I want to do is to print out the data inside each csv file.
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
// throw new ServletException(ex);
}
}
}
ZipInputStream is an InputStream, so you can read from it as normal:
while ((entry = zis.getNextEntry()) {
byte[] buf = new byte[1024];
int len;
while ((len = zis.read(buf)) > 0) {
// here do something with data in buf
}
Related
I've researched different solutions to this problem, but none of them works for me. I am trying to download a file from Firebase (which I am successful in doing) and then I am trying to open that file in my app right after the download completes. However, my app either crashes or does nothing.
Below is the code for downloading the file (from FirebaseStorage which works):
public void download(String name) {
final String pdf_name = name.substring(0, name.lastIndexOf('.'));
storageReference = firebaseStorage.getInstance().getReference();
ref=storageReference.child("Auctions/" + name);
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
String url = uri.toString();
downloadFile(ActiveAuctionsActivity.this, pdf_name, ".pdf", DIRECTORY_DOWNLOADS, url);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
SpannableString spannableString = new SpannableString("אין תיק עבודה למכרז זה");
spannableString.setSpan(
new ForegroundColorSpan(getResources().getColor(android.R.color.holo_red_light)),
0,
spannableString.length(),
0);
Toast.makeText(ActiveAuctionsActivity.this, spannableString, Toast.LENGTH_LONG).show();
}
});
}
public void downloadFile(Context context, String fileName, String fileExtention, String destinationDirectory, String url){
DownloadManager downloadmanager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDescription("מוריד.....");
//request.setDestinationInExternalFilesDir(context, destinationDirectory, fileName + fileExtention);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName + fileExtention);
// call allowScanningByMediaScanner() to allow media scanner to discover your file
request.allowScanningByMediaScanner();
downloadmanager.enqueue(request);
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Toast.makeText(getApplicationContext(), "מוריד את התיק העבודה.....",
Toast.LENGTH_SHORT).show();
}
After, I setup the receiver with the openFile() method:
BroadcastReceiver onComplete=new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Toast.makeText(getApplicationContext(), "ההורדה הסתיימה",
Toast.LENGTH_LONG).show();
openFile("GMU.pdf");
}
};
public void openFile(String fileName){
try {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
Uri path = Uri.fromFile(file);
Log.i("Fragment2", String.valueOf(path));
Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfOpenintent.setDataAndType(path, "application/pdf");
this.startActivity(pdfOpenintent);
} catch (ActivityNotFoundException e) {
Toast.makeText(ActiveAuctionsActivity.this, "error", Toast.LENGTH_LONG).show();
}
}
Again, the file does download, but doesn't open.
What am I doing wrong, could you please advise me?
EDIT
I also tried the code below as my openFile() but that also doesn't work:
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
Uri path = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file);
Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
pdfOpenintent.setDataAndType(path, pdfOpenintent.getType());
pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfOpenintent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
ActiveAuctionsActivity.this.startActivity(pdfOpenintent);
} catch (ActivityNotFoundException e) {
pdfOpenintent.setType("application/*");
startActivity(Intent.createChooser(pdfOpenintent, "No Application found to open File - " + fileName));
}
i have a rest api with spring boot that works fine it uploads an excel file in temporary file then it adds it to the database
However i am a beginner in angular Js so i am not able to do that even after a long search on the internet i will appreciate any help
here is my code for the rest API
#RestController
public class UploadController {
#Autowired
PosteDao posteDao ;
//Save the uploaded file to this folder
private static String UPLOADED_FOLDER = "C://Temp//";
#PostMapping("/doUploadFile") // //new annotation since 4.3
public String singleFileUpload(#RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
if (file.isEmpty()) {
redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
return "redirect:uploadStatus";
}
try {
// Get the file and save it somewhere
byte[] bytes = file.getBytes();
Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
Files.write(path, bytes);
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded '" + file.getOriginalFilename() + "'");
processExcel();
} catch (IOException e) {
e.printStackTrace();
}
return "greaat";
}
public String processExcel() throws IOException {
try {
InputStream ExcelFileToRead = new FileInputStream("C:\\\\list.xlsx");
//définir l'objet workbook
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
//Itérer sur le nombre de sheets
for(int i=0;i<wb.getNumberOfSheets();i++)
{
XSSFSheet sheet = wb.getSheetAt(i);
//Itérer sur les lignes
Row row;
for(int k=1;k<sheet.getLastRowNum()+1;k++)
{
row = (Row) sheet.getRow(k); //sheet number
int idPoste;
if( row.getCell(0)==null) { idPoste = 0; }
else idPoste= (int) row.getCell(0).getNumericCellValue();
String libelle;
if( row.getCell(1)==null) { libelle = "null";} //suppose excel cell is empty then its set to 0 the variable
else libelle = row.getCell(1).toString(); //else copies cell data to name variable
System.out.println(libelle);
System.out.println(idPoste);
}
}} catch (Exception e) {
System.err.println("Erreur");
// TODO Auto-generated catch block
e.printStackTrace();
}
return "data extracted successfully";
}
}
I was moving files from Google drive to blobstore using the below code . But now the FileWriteChannel is deprecated and the code is not working. Is there an alternative solution for this problem ?
private BlobKey getBlobKey(File f, DriveObject driveObject)
throws IOException, MalformedURLException {
Drive service = ((GoogleDrive) driveObject).getService();
byte[] buffer = new byte[(int) f.getFileSize().intValue()];
GenericUrl url = new GenericUrl(f.getDownloadUrl());
HttpResponse response = service.getRequestFactory()
.buildGetRequest(url).execute();
InputStream is = response.getContent();
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = null;
boolean lock = true;
try {
file = fileService.createNewBlobFile("application/zip");
FileWriteChannel writeChannel = fileService.openWriteChannel(
file, lock);
int len;
while ((len = is.read(buffer)) >= 0) {
ByteBuffer bb = ByteBuffer.wrap(buffer, 0, len);
writeChannel.write(bb);
}
writeChannel.closeFinally();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BlobKey bk = fileService.getBlobKey(file);
return bk;
}
You need to use Java Client library:
GcsOutputChannel outputChannel =
gcsService.createOrReplace(fileName, GcsFileOptions.getDefaultInstance());
outputChannel.write(ByteBuffer.wrap(content));
outputChannel.close();
I want to create a method that will load a txt file and then change it but thats another method.
private void openFile() {
fileChooser.getSelectedFile();
JFileChooser openFile = new JFileChooser();
openFile.showOpenDialog(frame);
}
What must go next in order to get data from the file after choosing it to manipulate its data?
The JFileChooser documentation has an example on how to continue your code, and get the name of the file chosen, which can then be turned into a File object. You should be able to modify that example to meet your needs:
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
Here's an example that might help you. I would want to read up on and try some simple examples on different buffers that will read and write. In fact, i have worked with these a lot in the last few months and I still have to go and look.
public class ReadWriteTextFile {
static public String getContents(File aFile) {
StringBuilder contents = new StringBuilder();
try {
BufferedReader input = new BufferedReader(new FileReader(aFile));
try {
String line = null; //not declared within while loop
while (( line = input.readLine()) != null){
contents.append(line);
contents.append(System.getProperty("line.separator"));
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
return contents.toString();
}
static public void setContents(File aFile,
String aContents)
throws FileNotFoundException,
IOException {
if (aFile == null) {
throw new IllegalArgumentException("File should not be null.");
}
if (!aFile.exists()) {
throw new FileNotFoundException ("File does not exist: " + aFile);
}
if (!aFile.isFile()) {
throw new IllegalArgumentException("Should not be a directory: " + aFile);
}
if (!aFile.canWrite()) {
throw new IllegalArgumentException("File cannot be written: " + aFile);
}
Writer output = new BufferedWriter(new FileWriter(aFile));
try {
output.write( aContents );
}
finally {
output.close();
}
}
public static void main (String... aArguments) throws IOException {
File testFile = new File("C:\\Temp\\test.txt");//this file might have to exist (I am not
//certain but you can trap the error with a
//TRY-CATCH Block.
System.out.println("Original file contents: " + getContents(testFile));
setContents(testFile, "The content of this file has been overwritten...");
System.out.println("New file contents: " + getContents(testFile));
}
}
I'm working on a servlet that accepts zipfile and will unzip and display the content of the csv files.
So far i'm able to display a few records. However, as shown in the image below, one of the record is diplaying "question marks"/unrecognised characters.
I checked the csv file and it's perfectly fine. I also tried to delete the text and typed some other text, but still unsuccessful.
image of the problem:
https://dl.dropbox.com/u/11910420/Screen%20Shot%202012-09-07%20at%203.18.46%20PM.png
public class AdminBootStrap extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream in = item.openStream();
if (item.isFormField()) {
out.println("Got a form field: "
+ item.getFieldName());
} else {
out.println("Got an uploaded file: "
+ item.getFieldName() + ", name = "
+ item.getName());
ZipInputStream zis = new ZipInputStream(
new BufferedInputStream(in));
ZipEntry entry;
// Read each entry from the ZipInputStream until no
// more entry found indicated by a null return value
// of the getNextEntry() method.
//
byte[] buf = new byte[5000];
int len;
String s = null;
while ((entry = zis.getNextEntry()) != null) {
out.println("Unzipping: " + entry.getName());
if (entry.getName().equalsIgnoreCase("booking.csv")) {
while ((len = zis.read(buf)) > 0) {
s = new String(buf);
String[] arrStr = s.split("\\n");
for (String a : arrStr) {
out.println(a);
}// end for
}
}
any ideas?
The culprit is s = new String(buf) because it decodes a string of bytes into a string of characters via a default encoding. Unfortunately the default encoding on GAE is US-ASCII.
You should the encoding of your CSV. For example for UTF-8 use:
s = new String(buf, "UTF-8");