[英]Openshift JBoss Character Encoding
我已經用j2ee開發了一個網站。 我在開發環境上的字符編碼沒有問題,但是當我將應用程序部署到openshift(jboss 7,mysql 5.5)時,UTF格式似乎無法正常工作。 我嘗試在服務器端登錄,似乎是jboss環境問題,因為我可以看到servlet無法解碼字符。 請在下面找到前端/后端的代碼,在代碼之后,我列出了我已經嘗試過的內容。
謝謝,
<form role="form" id="createadform" name="createadform" action="createads" method="POST" enctype="multipart/form-data">
<div class="row" >
<div class="col-md-6 col-md-offset-3" >
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Aşağıdaki formu doldurarak ilanınızı oluşturabilirsiniz.</h3>
</div>
<div class="panel-body ">
<div class="col-md-6" >
<div class="form-group has-feedback" style="width: 100%">
<label for="isim">Başlık (En fazla 25 karakter)</label>
<input type="text" class="form-control" id="headline" name="headline" maxlength="25" required>
</div>
<div class="form-group has-feedback">
<label for="minSalary">Maaş Alt Sınırı</label>
<input type="number" class="form-control" id="minsalary" name="minsalary" required>
</div>
<div class="form-group has-feedback">
<label for="maxSalary">Maaş Üst Sınırı</label>
<input type="number" class="form-control" id="maxsalary" name="maxsalary" required>
</div>
<div class="form-group has-feedback">
<label for="currency">Para Birimi</label>
<select class="form-control" id="currency" name="currency">
<c:forEach var="currency" items="${currencies}">
<option name="currency" value=${currency.currencyid}>${currency.currencydescription} </option>
</c:forEach>
</select>
</div>
<div class="form-group has-feedback" >
<label for="city">Şehir</label>
<select class="form-control" id="city" name="city">
<c:forEach var="city" items="${cities}">
<option name="city" value=${city.plateno}>${city.name} </option>
</c:forEach>
</select>
</div>
<div class="form-group has-feedback">
<label for="worktype">İş tipi</label>
<select class="form-control" id="worktype" name="worktype">
<c:forEach var="worktype" items="${worktypes}">
<option name="worktype" value=${worktype.worktypeid}>${worktype.name} </option>
</c:forEach>
</select>
</div>
<div class="col-md-6 text-center">
<input type="file" name="resim" id="resim" accept="image/*"/>
</div>
</div>
<div class="col-md-6" >
<div class="form-group has-feedback">
<label for="freetext">İş Tanımı</label>
<textarea class="form-control" style="height: 300px;" id="freetext" name="freetext"></textarea>
</div>
<button type="submit" style="float: right; " class="btn btn-info ">Kaydı oluştur</button>
</div>
</div>
</div>
</div>
</div>
</form>
我也有一個用於文件擴展名驗證的javascript:
<script>
$("#createadform").submit(function(e){
e.preventDefault();
var resim = $('resim');
var headline = $('headline');
var minsalary = $('minsalary');
var maxsalary = $('maxsalary');
var currency = $('currency');
var city = $('city');
var worktype = $('worktype');
var freetext = $('freetext');
if ($('#resim').hasExtension(['.jpg', '.jpeg', '.bmp', '.gif', '.png'])) {
this.submit();
}else{
alert("Seçeceğiniz resim için 'jpg, jpeg,bmp,gif ve png' formatlarını kullanabilirsiniz.");
}
});
</script>
我的servlet代碼:
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
Part resim = request.getPart("resim");
InputStream is = resim.getInputStream();
String resimFilename = FileUtilities.getFileName(resim);
String headline = request.getParameter("headline");
String freetext = request.getParameter("freetext");
System.out.println("FREETEXT in servlet : " + freetext);
int minsalary = Integer.parseInt(request.getParameter("minsalary"));
int maxsalary = Integer.parseInt(request.getParameter("maxsalary"));
int currencyId = Integer.parseInt(request.getParameter("currency"));
int cityId = Integer.parseInt(request.getParameter("city"));
int worktype = Integer.parseInt(request.getParameter("worktype"));
Person user = (Person) request.getSession().getAttribute("user");
int employeeId = user.getPersonid();
我已將以下屬性添加到我的standalone.xml文件中:
<system-properties>
<property name="org.apache.coyote.http11.Http11Protocol.COMPRESSION" value="on"/>
<property name="org.apache.catalina.connector.URI_ENCODING" value="UTF-8"/>
<property name="org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING" value="true"/>
</system-properties>
失敗之后,我將這些屬性移到了文件的末尾,而jboss無法啟動該時間。
我也已經在.openshift \\ action_hooks文件夾中創建了一個pre_start_jbossas-7文件,並在其中添加了以下幾行:
export JAVA_OPTS="$JAVA_OPTS -Dorg.apache.catalina.connector.URI_ENCODING=\"UTF-8\" \
-Dorg.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING=\"true\""
編輯1:
我也嘗試過這種解決方案,但它也不起作用。
無法在JBoss openshift中的JAVA_OPTS中設置選項
編輯2:
我已經閱讀了動作鈎子腳本部分,並使它們可執行,並嘗試再次部署,但仍然無法正常工作
我已經解決了這個問題,並想在這里登錄,以防萬一其他人搜索相同問題的答案。
首先,問題是關於多部分表單數據。 盡管Servlet 3.0 API讓我通過以下方式獲取參數
request.getParameter("parameterName")
servlet無法獲取UTF-8格式的字符串。 因此,我用以下幾行更改了此行,它的工作方式就像是魅力。
Part partname= request.getPart("parameterName");
InputStream is= partname.getInputStream();
String parameter = IOUtils.toString(is, "UTF-8");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.