Scale image after upload JSP - java

I need to know how i can scale the uploaded images and save it on the server in the Upload folder.
I have this to process the the form where people can upload there files.
public void init() {
fileSavePath = getServletContext().getRealPath("/") + File.separator + UPLOAD_DIRECTORY;/*save uploaded files to a 'Upload' directory in the web app*/
if (!(new File(fileSavePath)).exists()) {
(new File(fileSavePath)).mkdir(); // creates the directory if it does not exist
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
String resp = "";
int i = 1;
resp += "<br>Here is information about uploaded files.<br>";
try {
MultipartParser parser = new MultipartParser(request, 1024 * 1024 * 1024); /* file limit size of 1GB*/
Part _part;
while ((_part = parser.readNextPart()) != null) {
if (_part.isFile()) {
FilePart fPart = (FilePart) _part; // get some info about the file
String name = fPart.getFileName();
if (name != null) {
long fileSize = fPart.writeTo(new File(fileSavePath));
resp += i++ + ". " + fPart.getFilePath() + "[" + fileSize / 1024 + " KB]<br>";
} else {
resp = "<br>The user did not upload a file for this part.";
}
}
}// end while
} catch (java.io.IOException ioe) {
resp = ioe.getMessage();
}
request.setAttribute("message", resp);
getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
}
For example i need to rezise all the image to 100x100

First save it, then load it, then scale, then resave it.
try
{
BufferedImage img = ImageIO.read(new File(in_path));
Image scaled = img.getScaledInstance(100, 100, Image.SCALE_FAST);
ImageIO.write(scaled, "png", out_path);
}
catch (Exception ex)
{
System.out.println(ex.getMessage();
}
See http://docs.oracle.com/javase/7/docs/api/java/awt/Image.html for list of scaling methods that can be used in the third parameter of getScaledInstance

Related

Validator for an upload file

I need a validator for an upload file, for the moment I can upload all the files but I need a check that file is less than 10 MB and only text format such as ms word, txt, ppt, excel (not executable, might be harmful).
Do I have to use and libraries of java for that, or I don't know what, cause I am a junior. If anyone has any ideas that will be very nice.
I have seen some other similar question and i try out but none that can help me.
Ps: I am working on java spring.
Here is my code is compiled but not working is possible edit and also to check for the length.
class FileUploader implements Receiver, SucceededListener, FailedListener, ProgressListener {
private static final long serialVersionUID = 1L;
public File file;
public String filename;
#Override
public void updateProgress(long readBytes, long contentLength) {
UI ui = UI.getCurrent();
ui.access(() -> {
progressBar.setCaption("Uploaded: " + (float) readBytes / (float) contentLength * 100 + "%");
progressBar.setValue((float) readBytes / (float) contentLength);
progressBar.setVisible(true);
});
}
#Override
public void uploadFailed(FailedEvent event) {
UIHelper.showErrorNotification("File could not be uploaded");
}
#Override
public void uploadSucceeded(SucceededEvent event) {
try {
String savePath = "/var/ccpt_work_files/";
Path filePath = Paths.get(savePath);
if (Files.exists(filePath)) {
copyFiles("/tmp/" + event.getFilename(), savePath + event.getFilename(), savePath + event.getFilename());
} else {
File targetFile = new File(savePath);
if (!targetFile.mkdirs()) {
UIHelper.showErrorNotification("Couldn't create dir: " + targetFile);
} else {
copyFiles("/tmp/" + event.getFilename(), savePath + event.getFilename(), savePath + event.getFilename());
}
}
} catch (IOException e) {
UIHelper.showErrorNotification("File could not be uploaded");
}
UIHelper.showInformationNotification("File successfully uploaded");
}
private void copyFiles(String from, String to, String finalPath) throws IOException {
com.google.common.io.Files.copy(new File(from), new File(to));
uploadedFilePath = finalPath;
}
#Override
public OutputStream receiveUpload(String filename, String mimeType) {
this.filename = filename;
FileOutputStream fos = null;
try {
file = new File("/tmp/" + filename);
fos = new FileOutputStream(file);
if (!file.exists()) {
file.createNewFile();
}
} catch (final IOException e) {
UIHelper.showErrorNotification("File could not be stored in server");
return null;
}
return fos;
}
};
If you already have the File object of type java.io.File, you can just check for file size and mime type
boolean hasValidFileSize(File file, double maxFileSize) {
double bytes = file.length();
double megabytes = (kilobytes / 1024) / 1024;
if (megabytes > maxFileSize) {
return false;
}
return true;
}
For non harmful files, you can just check for the mime type. Look for ways on how to get the mime types for the files that you needed to be allowed and compare it with your file's mime type.
You can use this method to get the fileSize of a file.
static String getFileSizeMegaBytes(File file) {
return (double) file.length() / (1024 * 1024) + " mb";
}
Refer the post to get the file type.
File tyle extension

Best practices to upload large files by chunks in Spring boot

I have A big file and i want to upload that in Server side. it's very important when occured any problem (like interrupting the internet or power cut ...) if i retry to upload, file uploaded from resume and doesn't need to send file from beginning.
I try this approach with sending file chunks but it seems that's not a good way, because a send chunks(byte arrays) directly in response Entity and this isn't good idea.
whatever if anybody can develop this approach and make this code a better code with better performance i appreciate that. does anybody known Best practice way to doing that??
and if u like my code, vote me
thanks :)
RestController
#RestController
#RequestMapping("/files")
public class Controller {
#Autowired
private MyService service;
#PutMapping("/upload/resume")
public Mono<ResponseEntity> uploadWithResume(#RequestPart("chunk")byte[] chunk,
#RequestPart("fileName")String fileName,
#RequestParam("length")Long length
) throws ParseException {
try {
return service.fileResumeUpload(chunk, fileName, length);
} catch (IOException e) {
e.printStackTrace();
return Mono.just(ResponseEntity.status(HttpStatus.PERMANENT_REDIRECT).build());
}
}
#RequestMapping(value = "/get/uploaded/size", method = RequestMethod.HEAD)
public Mono<ResponseEntity> getUploadedSize(#RequestParam("fileName") String fileName) throws IOException {
if (Files.exists(Paths.get("src/main/resources/" + fileName))) {
String size = String.valueOf(Files.size(Paths.get("src/main/resources/" + fileName)));
return Mono.just(ResponseEntity.ok()
.header("upload-offset", size)
.build());
} else{
return Mono.just(ResponseEntity.notFound()
.header("upload-offset" , "0").build());
}
}
}
Service
public Mono<ResponseEntity> fileResumeUpload(byte[] chunk , String fileName,long length) throws IOException, ParseException {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("src/main/resources/" + fileName, true));
boolean uploaded = true;
try {
out.write(chunk);
} catch (IOException e) {
uploaded = false;
System.err.println("io exception");
} finally {
if (uploaded) {
out.close();
return Mono.just(ResponseEntity.ok()
.header("expiration-date", getExpirationDate())
.build());
} else {
out.close();
return Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build());
}
}
}
Sending chunks with webTestClient
#Test
public void test1_upload_Expected_200StatusCode(){
try {
String fileName = "film.mkv";
RandomAccessFile raf = new RandomAccessFile(new File("src/test/resources/" + fileName), "rw");
long realSize = raf.length();
List<String> strings = webTestClient.head().uri("/files/get/uploaded/size?fileName=" + fileName)
.exchange().expectBody().returnResult().getResponseHeaders().get("upload-offset");
long uploadedSize = Long.valueOf(strings.get(0));
boolean f = false;
int sizeBuffer = 256 * 1024;
byte[] buffer = new byte[sizeBuffer];
MultiValueMap<String, Object> formData;
WebTestClient.ResponseSpec exchange = null;
System.out.println("first uploaded Size ; " + uploadedSize);
raf.seek(uploadedSize);
while (raf.read(buffer) != -1) {
formData = new LinkedMultiValueMap<>();
formData.add("fileName", fileName);
formData.add("chunk", buffer);
formData.add("length", realSize);
exchange = webTestClient.put().uri("/files/upload/resume")
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(formData))
.exchange();
exchange.expectStatus().isOk();
if (exchange.expectBody().returnResult().getStatus().is5xxServerError()) {
return;
}
if (uploadedSize + 256 * 1024 > realSize) {
sizeBuffer = ((int) (realSize - uploadedSize));
System.out.println(sizeBuffer);
uploadedSize = uploadedSize + sizeBuffer;
System.out.println(uploadedSize);
buffer = new byte[sizeBuffer];
f=true;
} else uploadedSize = uploadedSize + sizeBuffer;
if (f) System.out.println(uploadedSize);
//System.out.println(uploadedSize);
float percent = ((float) uploadedSize / realSize * 100);
System.out.format("%.2f\n", percent);
}
if (exchange!=null)
exchange.expectStatus().isOk();
}
catch (Exception e){
e.printStackTrace();
System.err.println("channel closed!!!");
}
}

JasperReports generated in a Servlet (GWT) does not appear

I am developing an application in GWT, I am using the api to generate JasperReports reports, initially tried to make the generation via RPC, which returned to the client a string with the path of the pdf created, but that did not work, now I'm trying to generate report by a normal servlet, but the report is generated, nothing appears on the screen, and no error is found in the browser console.
Details:
dev mode works perfectly.
on localhost: 8080 works perfectly.
The error is when the application is published in an external Tomcat
Here are my code
Servlet:
public class RelatorioPacienteServiceImpl extends HttpServlet {
private static final long serialVersionUID = 1L;
private ServletContext sc;
public void init(ServletConfig config) throws ServletException {
super.init(config);
sc = config.getServletContext();
}
#SuppressWarnings({ "unused", "unchecked", "rawtypes" })
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = req.getParameter("id");
Map m = req.getParameterMap();
Paciente paciente = new Paciente();
File reportFile = null;
String dir = sc.getRealPath(sc.getContextPath().replaceAll("\\\\", "/"));
Map parameters = new LinkedHashMap();
String path = dir + "/../reports/";// tomcat
path = path.replaceAll("\\\\", "/");
try {
paciente = PacienteDAO.getPacientePorId(Integer.parseInt(id));
List<Paciente> list = new ArrayList<>();
list.add(paciente);
HashMap parametros = new HashMap<String, Boolean>();
parametros.put("cpf", NumberMask.formatCpf(paciente.getCpf()));
parametros.put("telefone1",NumberMask.formatPhone(paciente.getTelefone1()));
parametros.put("telefone2",NumberMask.formatPhone(paciente.getTelefone2()));
parametros.put("telefoneResponsavel",NumberMask.formatPhone(paciente.getTelefoneResponsavel()));
parametros.put("dataNascimento",StringUtil.formatDate(paciente.getDataNascimento()));
switch (paciente.getEtnia()) {
case EtniaProps.BRANCA:
parametros.put("etnia","Branco");
break;
case EtniaProps.INDIGENA:
parametros.put("etnia","Indigena");
break;
case EtniaProps.PARDA:
parametros.put("etnia","Parda");
break;
case EtniaProps.PRETA:
parametros.put("etnia","Preta");
break;
default:
break;
}
reportFile = new File(path + "report_paciente.jasper");
byte[] bytes = null;
JRDataSource jrds = new JRBeanCollectionDataSource(list);
try {
bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parametros, jrds);
} catch (JRException ex) {
ex.printStackTrace();
System.out.println("Erro ao gerar o relatório " + ex.getMessage());
}
if (!list.isEmpty()) {
if (bytes != null && bytes.length > 0) {
resp.setContentType("application/pdf");
resp.setContentLength(bytes.length);
ServletOutputStream outputStream = resp.getOutputStream();
outputStream.write(bytes, 0, bytes.length);
outputStream.flush();
outputStream.close();
}
} else {
resp.setContentType("text/html");
ServletOutputStream outputStream = resp.getOutputStream();
String mensagem = "<html>" + "<head>" + "<meta http-equiv=\"content-type\" charset=\"UTF-8\" content=\"text/html\">"
+ "<title>Incor lages</title>" + "</head>" + "<body>"
+ "<br><br><br><br><h1>Documento sem paginas" + "</body>" + "</html>";
outputStream.write(mensagem.getBytes(), 0, mensagem.getBytes().length);
resp.setContentLength(mensagem.getBytes().length);
outputStream.flush();
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Erro ao execura a query " + e.getMessage());
}
}
Calling servlet:
String url = GWT.getModuleBaseURL() + "relatorioPacienteService?id=" + paciente.getId();
Window.open(url, "_blank", "");
Any help would be appreciated
Can u print reportFile.getPath(). I doubt the path of .jasper file is incorrect.
First of all it would be even better If you can post your .jrxml file.
Based on the info available (report is generated, but blank), I think following is the area of concern:
paciente = PacienteDAO.getPacientePorId(Integer.parseInt(id));
List<Paciente> list = new ArrayList<>();
list.add(paciente);
Make sure PacienteDAO.getPacientePorId(Integer.parseInt(id)); is actually returning a bean. Beacuse If it does not return anything or returns null, the data source you use i.e. JRBeanCollectionDataSource, will have no data and hence nothing would be displayed.

Browsers freeze by file download

I got a problem when files were download , I can't get any actions or events when clicking any links , butttons and menues after download process was done.
Below is my codes for excel file download button ...
Button btnDownloadExcel = new Button("Excel Download");
btnDownloadExcel.addStyleName("downloadButton");
btnDownloadExcel.addClickListener(new ClickListener() {
#Override
public void buttonClick(final ClickEvent event) {
StringBuilder url = new StringBuilder("/myproject/filedownload.html?category=excel");
url.append("&seq=" + 111);
getUI().getPage().open(url.toString(), "_self");
}
});
Below is servlet for handle excel file download request (I used JExcel API for excel file)
#WebServlet(value = "/filedownload.html")
public class DownloadServletController extends HttpServlet {
private final Logger log = LoggerFactory.getLogger(DownloadServletController.class);
protected final void doGet(final HttpServletRequest request,
final HttpServletResponse response) throws ServletException, IOException {
String category = request.getParameter("category");
long seq = request.getParameter("seq") == null ? -1L : Long.parseLong(request.getParameter("seq"));
byte[] stream = null;
if (category.equals("excel")) {
try {
stream = getSampleExcelStream(seq);
}
catch (BusinessException e) {
log.error("Generating streams for " + category + " got Error !" + e);
}
ExcelSupport.createExcel("Test", seq, stream, response);
}
}
private byte[] getSampleExcelStream(final long seq) throws BusinessException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
String staticDir = System.getProperty("staticDir");
String templateDir = staticDir + "/templates/sample_excel_template.xls";
WorkbookSettings wsWrite = new WorkbookSettings();
wsWrite.setEncoding("UTF-8");
wsWrite.setAutoFilterDisabled(false);
WritableWorkbook workBook = Workbook.createWorkbook(baos, Workbook.getWorkbook(new File(templateDir)),
wsWrite);
workBook.write();
baos.close();
workBook.close();
}
catch (BiffException e) {
throw new BusinessException("Excel file Creating Error!");
}
catch (WriteException e) {
throw new BusinessException("Error ! writing excel file process has occured!");
}
catch (FileNotFoundException e) {
throw new BusinessException("FileNotFoundException, when getting stream for excel", e);
}
catch (IOException e) {
throw new BusinessException("IOException, when getting stream for excel", e);
}
return baos.toByteArray();
}
}
ExcelSupport.java is below
public final class ExcelSupport {
private ExcelSupport() {
}
private static final Logger LOGGER = LoggerFactory.getLogger(ExcelSupport.class);
public static void createExcel(final String fileNamePrefix, final long seq,
final byte[] stream, final HttpServletResponse response) {
StringBuffer fileName = new StringBuffer();
fileName.append(fileNamePrefix + "_");
if (seq > -1) {
fileName.append("(uid-" + seq + ")_");
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
fileName.append(sdf.format(new Date()));
fileName.append(".xls");
StringBuffer sbContentDispValue = new StringBuffer();
sbContentDispValue.append("inline");
sbContentDispValue.append("; filename=");
sbContentDispValue.append(fileName);
response.setContentType("application/msexcel");
response.addHeader("Cache-Control", "max-age=30");
response.addHeader("Content-disposition", sbContentDispValue.toString());
response.setContentLength(stream.length);
try {
ServletOutputStream osStream = response.getOutputStream();
osStream.write(stream);
osStream.flush();
osStream.close();
}
catch (IOException e) {
LOGGER.error("Creating Excel for " + fileName + " got Error !" + e);
}
}
}
Can somebody correct me what I am wrong ? Download process was fine , nothing error and I got excel file successfully. But I have no idea why browser was freeze. I can't see any error logs or messages in IDE console and browser's console. Thanks for reading my question !
PS : I am sure this codes work fine and did not freeze on other GWT projects.
Now I found the problem . I used Network console of Firefox 31 and here is screen-shoot for before download and here is after download. I notice that I lost all web datas because the replacing url by getUI().getPage().open(url.toString(), "_self");
So , if I use others instead of _self , everythings were fine but browsers were block popups. I can't tell the users to enable popups of their browsers . So , finally I use Link component as below ..
Link linkDownloadExcel = new Link("Excel Download", new ExternalResource(
"/myproject/filedownload.html?category=excel&seq=" + 111), "_blank", -1, -1, BorderStyle.DEFAULT);
linkDownloadExcel.addStyleName("downloadButton");
linkDownloadExcel.setIcon(new ExternalResource("/myproject/images/excel-icon.png"));
hlButtonLayout.addComponent(linkDownloadExcel);

GWT Upload by Manuel Carrasco Moñino Issue

I am using this gwt upload system here(http://code.google.com/p/gwtupload/). I am getting some problems with it.
Show to feed it with a path from the client
Get the path on the server where the file was saved
set a path on the server where the file is to be saved
This the servlet to handle the file upload
public class SampleUploadServlet extends UploadAction {
private static final long serialVersionUID = 1L;
Hashtable<String, String> receivedContentTypes = new Hashtable<String, String>();
/**
* Maintain a list with received files and their content types.
*/
Hashtable<String, File> receivedFiles = new Hashtable<String, File>();
/**
* Override executeAction to save the received files in a custom place
* and delete this items from session.
*/
#Override
public String executeAction(HttpServletRequest request, List<FileItem> sessionFiles) throws UploadActionException {
String response = "";
int cont = 0;
for (FileItem item : sessionFiles) {
if (false == item.isFormField()) {
cont ++;
try {
/// Create a new file based on the remote file name in the client
// String saveName = item.getName().replaceAll("[\\\\/><\\|\\s\"'{}()\\[\\]]+", "_");
// File file =new File("/tmp/" + saveName);
/// Create a temporary file placed in /tmp (only works in unix)
// File file = File.createTempFile("upload-", ".bin", new File("/tmp"));
/// Create a temporary file placed in the default system temp folder
File file = File.createTempFile("upload-", ".bin");
item.write(file);
/// Save a list with the received files
receivedFiles.put(item.getFieldName(), file);
receivedContentTypes.put(item.getFieldName(), item.getContentType());
/// Compose a xml message with the full file information which can be parsed in client side
response += "<file-" + cont + "-field>" + item.getFieldName() + "</file-" + cont + "-field>\n";
response += "<file-" + cont + "-name>" + item.getName() + "</file-" + cont + "-name>\n";
response += "<file-" + cont + "-size>" + item.getSize() + "</file-" + cont + "-size>\n";
response += "<file-" + cont + "-type>" + item.getContentType()+ "</file-" + cont + "type>\n";
} catch (Exception e) {
throw new UploadActionException(e);
}
}
}
/// Remove files from session because we have a copy of them
removeSessionFileItems(request);
/// Send information of the received files to the client.
return "<response>\n" + response + "</response>\n";
}
/**
* Get the content of an uploaded file.
*/
#Override
public void getUploadedFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
String fieldName = request.getParameter(PARAM_SHOW);
File f = receivedFiles.get(fieldName);
if (f != null) {
response.setContentType(receivedContentTypes.get(fieldName));
FileInputStream is = new FileInputStream(f);
copyFromInputStreamToOutputStream(is, response.getOutputStream());
} else {
renderXmlResponse(request, response, ERROR_ITEM_NOT_FOUND);
}
}
/**
* Remove a file when the user sends a delete request.
*/
#Override
public void removeItem(HttpServletRequest request, String fieldName) throws UploadActionException {
File file = receivedFiles.get(fieldName);
receivedFiles.remove(fieldName);
receivedContentTypes.remove(fieldName);
if (file != null) {
file.delete();
}
}
}
Thanks
Try with this:
public String executeAction(HttpServletRequest request, List<FileItem> sessionFiles) throws UploadActionException {
for (FileItem item : sessionFiles) {
if (false == item.isFormField()) {
String uploadedFileName = "";
try {
String uploadsDir = "/uploads";
File dirFile = new File(uploadsDir);
dirFile.mkdirs();
String filename = FilenameUtils.getName(item.getName()); // uploaded file filename
File file = new File(uploadsDir, filename);
item.write(file);
uploadedFileName = uploadsDir + "/" + filename;
} catch (Exception e) {
logger.error("ERROR UPLOADING FILE: " + uploadedFileName + ", Exception: " + e);
throw new UploadActionException(e.getMessage());
}
}
removeSessionFileItems(request);
}
return null;
}
Happy coding!
Regards.

Categories

Resources