掲示板

upload a file with persian name in liferay 6.2 portlet on centos linux serv

thumbnail
7年前 に behnaz eslami によって更新されました。

upload a file with persian name in liferay 6.2 portlet on centos linux serv

Junior Member 投稿: 54 参加年月日: 11/11/14 最新の投稿
In my custom liferay6.2 portlet, I have a form for uploading files on the folder in my CentOS linux server. Each time that I upload a file with persian name, it uploads successfully but after that I check destination folder in server I see that the name file becomes change and instead the exact name in persian language some strange characters replace on the main name.

for example my file name is نحوه ورود به سیستم.doc
but after upload, I checked the folder on the server and I see that this file uploaded with this name : طµظ†ط¯ظˆظ‚ ط³ط±ظ…ط§ظٹظ‡ ع¯ط°ط§ط±ظٹ ط¨ط§ ط¯ط±ط¢ظ…ط¯ ط«ط§ط¨طھ ظƒظˆط«ط± ظٹظƒظ….doc

How can I solve this issue?
This is my view.jsp page
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
/**
* Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
%>

<link rel="stylesheet" href="<%=request.getContextPath()%>/css/theme.css">
<script src="<%=request.getContextPath()%>/js/jalali.js"></script>
<script src="<%=request.getContextPath()%>/js/calendar.js"></script>
<script src="<%=request.getContextPath()%>/js/calendar-setup.js"></script>
<script src="<%=request.getContextPath()%>/js/calendar-fa.js"></script>



<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib prefix="liferay-ui" uri="http://liferay.com/tld/ui" %>
<%@ taglib prefix="aui" uri="http://liferay.com/tld/aui" %>

<portlet:defineObjects />

<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

<style type="text/css">
fieldset {
border: 1px solid #CCCCCC !important;
margin: 10px 10px 14px !important;
padding: 10px !important;
}
.aui legend {
display: block;
width: auto !important;
padding: 0 10px !important;
margin-bottom: 20px;
font-size: 19.5px;
line-height: 40px;
color: #333;
border: 0;
border-bottom: 0px none !important;
}
.aui-field-label { width: 190px; float: right; }

[class~="aui-form-validator-message"] { position: relative !important; height: 20px; }

.aui-field-eblagh-date .form-validator-stack,
.aui-field-eblagh-subject .form-validator-stack,
.aui-field-eblagh-type .form-validator-stack,
.aui-field-shenaseh-num .form-validator-stack,
.aui-field-doc-code .form-validator-stack,
.aui-field-baznegari-num .form-validator-stack,
.aui-field-eblagh-status .form-validator-stack,
.aui-field-process-owner .form-validator-stack,
.aui-field-eblagh-validity .form-validator-stack,
.aui-field-attachment-file .form-validator-stack
{ display: none; }


select.error-field { border-color: #990000; border-style: dashed; }

.aui-field-eblagh-date .error-field,
.aui-field-eblagh-subject .error-field,
.aui-field-shenaseh-num .error-field,
.aui-field-doc-code .error-field,
.aui-field-baznegari-num .error-field {
border-color: #990000 !important;
}

.aui-field-attachment-file .error-field {
border-color: #990000 !important;
border: 1px solid;
}

.aui-form-validator-error-container { color: #000000 !important; }
.aui-field-label span { color: #ff0000; }

.bottuns { padding-top: 20px; float: left; }

</style>

<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

<script type="text/javascript">
function Validate()
{
var attachment_file =document.getElementById("attachment_file").value;
if(attachment_file!='')
{
var checkFile = attachment_file.toLowerCase();
var file = document.getElementById("attachment_file");
if(file.files[0].size > 53477376) // validation according to file size
{
document.getElementById("errorMsg").innerHTML="حجم سند مورد نظر، بیشتر از 50 مگابایت می باشد.";
return false;
} else {
document.getElementById("errorMsg").innerHTML="<img src='<%=request.getContextPath()%>/images/Success-icon.png' />";
return true;
}
}
}
</script>

<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->


<%
if(request.getAttribute("error_file_exist") != null)
{
out.print("<ul>");
out.print("<li>");
out.print("سندی با این نام وجود دارد، لطفاً نام سند خود را تغییر داده و مجدداً تلاش نمایید.");
out.print("</li>");
out.print("</ul>");
}
else if (request.getAttribute("error_file_size") != null)
{
out.print("<ul>");
out.print("<li>");
out.print("حجم سند انتخابی زیاد می باشد.");
out.print("</li>");
out.print("</ul>");
}
else if (request.getAttribute("success_msg") != null)
{
out.print("<p>");
out.print("<img src='/upload-file-portlet/images/Success-icon.png' />");
out.print("<span style='font-size: 14px; padding-right: 10px;'>");
out.print("سند مورد نظر با موفقیت اضافه گردید.");
out.print("</span>");
out.print("</p>");
}
%>

<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->


<portlet:defineObjects />
<portlet:actionURL var="myUrl">
</portlet:actionURL>




<form enctype="multipart/form-data" action="<%= myUrl %>" method="POST" name="fm" id="fm" onChange="Validate()">
<div id="page-wrap">
<div id="tabs">
<fieldset>
<legend>مشخصات فایل ارسالی</legend>
<p class="aui-field-eblagh-date">
<label class="aui-field-label">تاریخ ابلاغ :<span>*</span></label>
<input name="eblagh_date" id="eblagh_date" autocomplete="off" type="text" /><img id="date_btn_1" src="<%=request.getContextPath()%>/images/cal.png" style="vertical-align: top;" />
<script type="text/javascript">
Calendar.setup({
inputField : "eblagh_date", // id of the input field
button : "date_btn_1", // trigger for the calendar (button ID)
ifFormat : "%Y-%m-%d", // format of the input field
dateType : 'jalali',
weekNumbers : false
});
</script>
</p>
<p class="aui-field-eblagh-subject">
<label class="aui-field-label">موضوع ابلاغیه :<span>*</span></label>
<input name="eblagh_subject" type="text" />
</p>
<p class="aui-field-eblagh-type">
<label class="aui-field-label">نوع ابلاغیه :<span>*</span></label>
<select id="eblagh_type" name="eblagh_type">
<option value="">انتخاب کنید</option>
<option value="فرم">فرم</option>
<option value="دستورالعمل">دستورالعمل</option>
<option value="روش اجرایی">روش اجرایی</option>
<option value="بخشنامه">بخشنامه</option>
</select>
</p>
<p class="aui-field-shenaseh-num">
<label class="aui-field-label">شماره شناسه :<span>*</span></label>
<input name="shenaseh_num" type="text" />
</p>

<p class="aui-field-doc-code">
<label class="aui-field-label">کد مدرک :<span>*</span></label>
<input name="doc_code" type="text" />
</p>

<p class="aui-field-baznegari-num">
<label class="aui-field-label">شماره بازنگری :<span></span></label>
<input name="baznegari_num" type="text" />
</p>

<p class="aui-field-eblagh-status">
<label class="aui-field-label">ماهیت ابلاغیه :<span>*</span></label>
<select id="eblagh_status" name="eblagh_status">
<option value="">انتخاب کنید</option>
<option value="عملیاتی">عملیاتی</option>
<option value="غیر عملیاتی">غیرعملیاتی</option>
</select>
</p>

<p class="aui-field-process-owner">
<label class="aui-field-label">مالک فرآیند :<span>*</span></label>
<select id="process_owner" name="process_owner">
<option value="">انتخاب کنید</option>
<option value="صندوق">صندوق</option>
<option value="تسهیلات">تسهیلات</option>
<option value="حسابداری">حسابداری</option>
<option value="مسئول IT">مسئول IT</option>
<option value="سایر">سایر</option>
</select>
</p>

<p class="aui-field-eblagh-validity">
<label class="aui-field-label">اعتبار ابلاغیه :<span>*</span></label>
<select id="eblagh_validity" name="eblagh_validity">
<option value="">انتخاب کنید</option>
<option value="جاری">جاری</option>
<option value="آرشیو">آرشیو</option>
</select>
</p>

<p class="aui-field-attachment-file">
<label class="aui-field-label">ضمیمه :<span>*</span></label>
<input name="attachment_file" id="attachment_file" type="file" />
<span id="errorMsg" style="position: relative; right: 1%; color: red; font-size: 14px;"></span>
</p>

<p>
<input type="submit" value="ذخیره" />
</p>
</fieldset>
</div>
</div>
</form>

<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

<aui:script>

AUI().ready('aui-form-validator', 'aui-overlay-context-panel', function(A) {

var validator2 = new A.FormValidator({
boundingBox: document.fm,

fieldContainer: 'p',
rules: {
eblagh_date: {
required: true
},
eblagh_subject: {
required: true,
rangeLength: [2,100]
},
eblagh_type: {
required: true
},
shenaseh_num: {
required: true,
digits: true
},
doc_code: {
required: true
},
baznegari_num: {
digits: true
},
eblagh_status: {
required: true
},
process_owner: {
required: true
},
eblagh_validity: {
required: true
},
attachment_file: {
required: true
}
},

on: {
submitError: function(event) {
var formEvent = event.validator.formEvent;
var errors = event.validator.errors;
},

submit: function(event) {
var formEvent = event.validator.formEvent;
return false;
}
}
});
});

</aui:script>
----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------

This is my JSPPortlet.jsp page {Generic portlet}
/**
* Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.sample.jsp.portlet;


import com.ghasemkiani.util.icu.PersianDateFormat;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.util.PortalUtil;
import kosar.dao.uploadFileDao;
import kosar.model.uploadFileModel;
import org.apache.commons.io.FilenameUtils;


import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.io.*;
import java.util.Date;
import javax.portlet.*;

/**
* <a href="JSPPortlet.java.html"><b><i>View Source</i></b></a>
*
* @author Brian Wing Shun Chan
*
*/
public class JSPPortlet extends GenericPortlet {

protected String realPath=null;

/**
* Helper method to serve up the mandatory view mode.
*/
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
PortletPreferences prefs = request.getPreferences();




PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/view.jsp");
dispatcher.include(request, response);
}


/**
* Called by the portlet container to allow the portlet to process an action request.
* This method is called if the client request was originated by a URL created
* (by the portlet) with the RenderResponse.createActionURL() method.
*/
public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
PortletPreferences prefs = request.getPreferences();
UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(request);



//convert Jalali to Gregorian
PersianDateFormat persianDateFormat = new PersianDateFormat("yyyy-MM-dd");
Date eblaghDate = null;
try {
eblaghDate = persianDateFormat.parse(uploadPortletRequest.getParameter("eblagh_date"));
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(eblaghDate);
System.out.println("convert Jalali to Gregorian");
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.MONTH) + 1);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));




String source= calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("source : " + source);




String gregorian_eblagh_date = source;
java.util.Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(gregorian_eblagh_date);
} catch (ParseException e) {
e.printStackTrace();
}
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
System.out.println(sqlDate);


String folder=getInitParameter("uploadFolder");

String eblagh_subject = uploadPortletRequest.getParameter("eblagh_subject");
String eblagh_type = uploadPortletRequest.getParameter("eblagh_type");
String shenaseh_num = uploadPortletRequest.getParameter("shenaseh_num");
String doc_code = uploadPortletRequest.getParameter("doc_code");
String baznegari_num= uploadPortletRequest.getParameter("baznegari_num");
String eblagh_status = uploadPortletRequest.getParameter("eblagh_status");
String process_owner = uploadPortletRequest.getParameter("process_owner");
String eblagh_validity = uploadPortletRequest.getParameter("eblagh_validity");
String attachment_addr;
String sourceFileName_attach = uploadPortletRequest.getFileName("attachment_file");
String ext = FilenameUtils.getExtension(sourceFileName_attach);

System.out.println("!!!!!!!!! -----> " + ext);

uploadFileModel fileModel = new uploadFileModel();
fileModel.setEblagh_date(sqlDate);
fileModel.setEblagh_subject(eblagh_subject);
fileModel.setEblagh_type(eblagh_type);
fileModel.setShenaseh_num(shenaseh_num);
fileModel.setDoc_code(doc_code);
fileModel.setBaznegari_num(baznegari_num);
fileModel.setEblagh_status(eblagh_status);
fileModel.setProcess_owner(process_owner);
fileModel.setEblagh_validity(eblagh_validity);
fileModel.setFile_name(sourceFileName_attach);
fileModel.setFile_extension(ext);

if (sourceFileName_attach != null && sourceFileName_attach != "") {
attachment_addr = folder + sourceFileName_attach;
}
else {
attachment_addr = "*****************";
}

try {
if (AddFile(request,uploadPortletRequest) == true) {
uploadFileDao fileDao = new uploadFileDao();
fileDao.addFileInfo(fileModel, attachment_addr);
}
else {
System.out.println("Sorry, Try Again !!!!!!!!!!!!!!!!!!!!!! - (AddFile)");
}
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}

}





/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

public boolean AddFile(ActionRequest request, UploadPortletRequest uploadPortletRequest) throws Exception{
boolean result = false;
String folder=getInitParameter("uploadFolder");
realPath = getPortletContext().getRealPath("/");
byte[] bytes = null;
String sourceFileName = uploadPortletRequest.getFileName("attachment_file");
File file = uploadPortletRequest.getFile("attachment_file");
long size = uploadPortletRequest.getSize("attachment_file");

System.out.println("############ ---> " + size);
bytes = FileUtil.getBytes(file);
File newFile = null;
if ((bytes == null)) { result = true; }
if ((bytes != null) && (bytes.length > 0)) {

try {
//sourceFileName = URLEncoder.encode(sourceFileName,"UTF-8");
System.out.println("----------> " + sourceFileName);
newFile = new File(folder+sourceFileName);
// 51 Mb = 53477376 byte
if(newFile.exists()){
System.out.println("File Exsist, Try again !!!");
request.setAttribute("error_file_exist","File Exsist, Try again !!!");

}
else if (size > 53477376) {
System.out.println("file size is too large!!!");
request.setAttribute("error_file_size", "file size is too large!!!");
} else {
System.out.print("photo-address ----------> " + folder + sourceFileName);
FileInputStream fileInputStream = new FileInputStream(file);
FileOutputStream fileOutputStream = new FileOutputStream(newFile);
fileInputStream.read(bytes);
fileOutputStream.write(bytes, 0, bytes.length);
fileOutputStream.close();
fileInputStream.close();
SessionMessages.add(request, "success");
result = true;
request.setAttribute("success_msg", "File added successfully.");

}
}
catch (FileNotFoundException e) {
System.out.println("File Not Found.");
e.printStackTrace();
SessionMessages.add(request, "error");
}
catch (IOException e1){
System.out.println("Error Reading The File.");
e1.printStackTrace();
SessionMessages.add(request, "error");
}
}
return result;

}

}
thumbnail
7年前 に Olaf Kock によって更新されました。

RE: upload a file with persian name in liferay 6.2 portlet on centos linux

Liferay Legend 投稿: 6403 参加年月日: 08/09/23 最新の投稿
behnaz eslami:
In my custom liferay6.2 portlet, I have a form for uploading files on the folder in my CentOS linux server.


Please link your crossposts (or stop crossposting) (why? ) and use the formatting options to make your code readable.