我目前正在使用jQuery-File-Upload.我可以上传一些带有日文或中文文件名的文件,我可以看到文件名是例如浏览器调试模式中的"お疲れ様です.txt"或"测试文档.txt",但是在后端(Java) ,它们变成了" -ãã,Œæ§~ã §ã ™.txt"和"æμ<试æ-‡æ¡£.txt".
我曾经尝试将formAcceptCharset设置为UTF-8,但它不起作用.
问题:
解析MultipartFormData时如何在Java端获取正确的文件名?
提前致谢.
BTW,以下是我的数据
-----------------------------25382434931419
Content-Disposition: form-data; name="file"; filename="�疲れ様��.txt"
Content-Type: text/plain
....
Run Code Online (Sandbox Code Playgroud)
添加Java代码
实际上我目前在Java端没有做任何事,
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String upload(InMultiPart inMP) {
while (inMP.hasNext()) {
InPart part = inMP.next();
MultivaluedMap<String, String> headers = part.getHeaders();
String fileName = null;
if (!headers.containsKey("Content-Disposition")) {
continue;
} else {
// get the file name here
fileName = parseFileName(headers.getFirst("Content-Disposition"));
}
//.....
}
//......
}
private String parseFileName(String disposition) {
int fileNameIndex = disposition.indexOf("filename=");
if …Run Code Online (Sandbox Code Playgroud) 我的基于ZF2的应用程序和Backbone在前端有问题.在某个地方,我跑步
this.model.save({
city_id: parseInt( this.$el.find( '#city_id' ).val() ),
from: this.$el.find( '#from' ).val(),
to: this.$el.find( '#to' ).val(),
price: parseInt( this.$el.find( '#price' ).val() )
});
Run Code Online (Sandbox Code Playgroud)
我打开Chrome嗅探器并查看请求详细信息:
PUT /account/trip/2 HTTP/1.1
Host: jamydays.ru
Connection: keep-alive
Content-Length: 186
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://jamydays.ru
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31
Content-Type: application/json
Referer: http://jamydays.ru/account
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: PHPSESSID=pekjbefmi1jn01q5fgm4gu6jk0; _ym_visorc=w
Run Code Online (Sandbox Code Playgroud)
请求有效负载是:
{"from_formatted":"10 маÑ","to_formatted":"19 маÑ","url":"/account/trip","id":2,"city_id":65170,"city":"Baardheere","from":"10-05-2013","to":"19-05-2013","price":500,"is_active":1}
Run Code Online (Sandbox Code Playgroud)
用于处理此请求的Conroller运行适当的操作:
class TripController extends AbstractRestfulController{
...
public …Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用角度x可编辑.http://vitalets.github.io/angular-xeditable/#editable-row
除验证错误显示外,任何工作都正常.这是我的HTML模板:
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Width</th>
<th>Length</th>
<th>Sheets quantity</th>
<th>Low price</th>
<th>High price</th>
<th>Currency</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="material in sheetMaterials">
<td>
<span editable-text="material.name" e-name="name" e-form="form" e-required>
{{ material.name }}
</span>
</td>
<td>
<span editable-text="material.width" e-name="width" e-form="form" e-required>
{{ material.width }}
</span>
</td>
<td>
<span editable-text="material.length" e-name="length" e-form="form" e-required>
{{ material.length }}
</span>
</td>
<td>
<span editable-text="material.sheets" e-name="sheets" e-form="form" e-required>
{{ material.sheets }}
</span>
</td>
<td>
<span editable-text="material.priceLow" e-name="priceLow" e-form="form" e-required>
{{ material.priceLow }}
</span>
</td> …Run Code Online (Sandbox Code Playgroud) 假设,我有MSWord文件source.doc,下一个内容是"Microsoft Word文件的内容".例如,我想通过PHP打开它并将单词"Microsoft"替换为"Openoffice"并将结果保存到result.doc中.以下代码使用preg_replace:
$content = file_get_contents( SOMEPATH . '/source.doc' );
$new_content = preg_replace( '/Microsoft/i', 'Openoffice', $content );
file_put_contents( SOMEPATH . '/target.doc', $new_content );
Run Code Online (Sandbox Code Playgroud)
或使用str_replace:
$content = file_get_contents( SOMEPATH . '/source.doc' );
$new_content = str_replace( 'Microsoft', 'Openoffice', $content );
file_put_contents( SOMEPATH . '/target.doc', $new_content );
Run Code Online (Sandbox Code Playgroud)
它们都不起作用.代码运行没有任何异常,但target.doc与source.doc相同.替换不执行.
我尝试了很多不同的接收器,比如正则表达式修改器,iconv等等,但没有任何帮助.
var_dump的$content示出的原始结构source.doc即充满不寻常的字符和作为我想一些它停止str_replace或preg_replace扫描.无法弄清楚它是哪个字符,如果我找到它我该怎么办.
var_dump的$new_content是相同的$内容.
感谢您的帮助!
angularjs ×1
backbone.js ×1
encoding ×1
file-upload ×1
http-put ×1
java ×1
javascript ×1
ms-word ×1
php ×1
preg-replace ×1
str-replace ×1
x-editable ×1