读书人

android系统上传资料可以成功接收但是

发布时间: 2013-07-24 14:36:29 作者: rapoo

android系统上传文件可以成功接收但是会出现文件名乱码的情况 代码贴出 求指点
android客户端核心代码

private void uploadFile()
{

String uploadUrl = "http://10.100.0.11:8080/upload_file_service/UploadServlet";
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try
{
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);

DataOutputStream dos = new DataOutputStream(httpURLConnection
.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
dos
.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
+ srcPath.substring(srcPath.lastIndexOf("/") + 1)
+ "\"" + end);
dos.writeBytes(end);

FileInputStream fis = new FileInputStream(srcPath);
byte[] buffer = new byte[8192]; // 8k
int count = 0;
while ((count = fis.read(buffer)) != -1)
{
dos.write(buffer, 0, count);

}


fis.close();

dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();

InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = br.readLine();

Toast.makeText(this, result, Toast.LENGTH_LONG).show();
dos.close();
is.close();

} catch (Exception e)
{
e.printStackTrace();
setTitle(e.getMessage());
}

}


服务器端核心代码如下
try
{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();


FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);

List<FileItem> items = upload.parseRequest(request);

String uploadPath = "d:\\upload\\";
File file = new File(uploadPath);
if (!file.exists())
{
file.mkdir();
}
String filename = "miui_pic";
InputStream is = null;
for (FileItem item : items)
{

if (item.isFormField())
{
if (item.getFieldName().equals("filename"))
{
// 如果新文件不为空,将其保存在filename中
if (!item.getString().equals(""))
filename = item.getString("UTF-8");
}
}

else if (item.getName() != null && !item.getName().equals(""))
{
filename = item.getName().substring(
item.getName().lastIndexOf("\\") + 1);
is = item.getInputStream();
}
}

filename = uploadPath + filename;

if (new File(filename).exists())
{
new File(filename).delete();


}

if (!filename.equals(""))
{
FileOutputStream fos = new FileOutputStream(filename);
byte[] buffer = new byte[8192];
int count = 0;

while ((count = is.read(buffer)) > 0)
{
fos.write(buffer, 0, count);

}
fos.close();
is.close();
out.println("文件上传成功!");

}
}
catch (Exception e)
{

}


[解决办法]
引用
"Content-Disposition: form-data; name=\"file\"; filename=\"" ...

文件名URLEncode一下?

读书人网 >Android

热点推荐