Java 将文件上载到服务器会使用无效字符保存文件
我正在从java应用程序上传一个文件到php服务器,它可以很好地上传所有内容,但我注意到如果它有一个字符,比如“in it”,那么文件中的字符将是“not” 例如,如果我使用以下java代码上传文件:Java 将文件上载到服务器会使用无效字符保存文件,java,php,file-upload,Java,Php,File Upload,我正在从java应用程序上传一个文件到php服务器,它可以很好地上传所有内容,但我注意到如果它有一个字符,比如“in it”,那么文件中的字符将是“not” 例如,如果我使用以下java代码上传文件: public static void main(String[] args) { FILE tmpFile = new FILE(EncryptionUtil.EncryptAndZip("tes't file.txt").getAbsoluteFile().toString());
public static void main(String[] args)
{
FILE tmpFile = new FILE(EncryptionUtil.EncryptAndZip("tes't file.txt").getAbsoluteFile().toString());
postData
(
UPLOAD_URL + "?filename=" + URLEncoder.encode(file.getFileName() + ".zip", "utf-8"),
tmpFile.getFileName() + ".zip",
tmpFile.getFileLoader().readAllBytes() // FILE.getFileLoader().readAllBytes() is just a wrapper and reads all bytes from a file to byte[]
);
}
private static final String CrLf = "\r\n";
public static void postData(String url, String filename, byte[] byteData) throws IOException
{
URLConnection conn = null;
InputStream is = null;
OutputStream os = null;
try
{
URL obj = new URL(url);
System.out.println("url:" + obj);
conn = obj.openConnection();
conn.setDoOutput(true);
String message1 = "";
message1 += "-----------------------------4664151417711" + CrLf;
message1 += "Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + filename + "\""
+ CrLf;
message1 += "Content-Type: application/octet-stream" + CrLf;
message1 += CrLf;
String message2 = "";
message2 += CrLf + "-----------------------------4664151417711--"
+ CrLf;
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=---------------------------4664151417711");
conn.setRequestProperty("Content-Length", String.valueOf((message1
.length() + message2.length() + byteData.length)));
os = conn.getOutputStream();
os.write(message1.getBytes());
int index = 0;
int size = 1024;
do
{
if ((index + size) > byteData.length)
{
size = byteData.length - index;
}
os.write(byteData, index, size);
index += size;
}
while (index < byteData.length);
os.write(message2.getBytes());
os.flush();
is = conn.getInputStream();
char buff = 512;
int len;
byte[] data = new byte[buff];
do
{
len = is.read(data);
if (len > 0)
{
System.out.println(new String(data, 0, len));
}
}
while (len > 0);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
os.close();
}
catch (Exception e)
{
}
try
{
is.close();
}
catch (Exception e)
{
}
try
{
}
catch (Exception e)
{
}
}
}
下面是我的php文件upload.php
<?php
$target_path = "" . $_GET['filename'];
echo ' name of file user wants to save as: '.$target_path.'<br/>';
if (!file_exists(dirname($target_path))) {
mkdir(dirname($target_path), 0777, true);
}
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "actual save name: ".basename( $_FILES['uploadedfile']['name'])." <br/>";
} else{
echo "There was an error uploading the file, please try again!<br />";
}
print_r($_FILES['uploadedfile']);
?>
是什么导致了这个问题?我认为是php试图清理输入?我该如何阻止这种情况发生?如OP所愿,结束这个问题 使用 它有助于取消引用带引号的字符串,这是原因 如果需要保存为tes't,并且如果需要,它会将tes't更改为tes't 从: stripslashes的一个示例用法是,当PHP指令在PHP5.4之前默认为on时,您不会将此数据插入需要转义的数据库等位置。例如,如果您只是直接从HTML表单输出数据
我通常只想关闭魔术报价,如果它是开着的。顺便说一句,它在较新的PHP版本中已经被弃用了。首先不允许使用这些字符。用于将“”替换为下划线,或不替换为任何内容。你也可以试试@Fred ii-stripslashes有效,因为某些原因我以为我已经关闭了自动卫生设备。太好了,很高兴听到它。@Fred ii-你能把答案贴出来让我接受吗?根据你的要求贴出来,干杯我以为我已经关掉了magic_quotes_gpc,这就是为什么我对逃跑的原因感到困惑。@user3045798我很高兴我们找到了问题的根源:
<?php
$target_path = "" . $_GET['filename'];
echo ' name of file user wants to save as: '.$target_path.'<br/>';
if (!file_exists(dirname($target_path))) {
mkdir(dirname($target_path), 0777, true);
}
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "actual save name: ".basename( $_FILES['uploadedfile']['name'])." <br/>";
} else{
echo "There was an error uploading the file, please try again!<br />";
}
print_r($_FILES['uploadedfile']);
?>