如何得到上传文件的文件名,在线等待。

abc优希 2010-12-27 07:59:56
我的代码:(可以不用看)
String path = getClass().getResource("").getPath();
int index = path.lastIndexOf("WEB-INF");
path = path.substring(1, index);
path = path + "upload/";
String contentType = request.getContentType();
int contentTypeLength = request.getContentLength();
int boundaryIndex = contentType.indexOf("boundary=");
String boundary = contentType.substring(boundaryIndex + 9);
int boundaryStrLength = boundary.length();

ServletInputStream serIn = request.getInputStream();
int line;
byte[] buffer = new byte[4096];
while ((line = serIn.readLine(buffer, 0, contentTypeLength)) != -1) {
if (buffer[0] == 13 | buffer[0] == 10)
break;
}
ByteArrayOutputStream byteOut = new ByteArrayOutputStream(
contentTypeLength);
int len;
while ((len = serIn.read(buffer)) >= 0) {
byteOut.write(buffer, 0, len);
}
byte[] outBytes = byteOut.toByteArray();
BufferedOutputStream fileOut = new BufferedOutputStream(
new FileOutputStream(path + "uploaded_file"));
fileOut.write(outBytes, 0, outBytes.length - boundaryStrLength - 8);
fileOut.flush();
fileOut.close();
...全文
146 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
abc优希 2010-12-27
  • 打赏
  • 举报
回复
自己搞定
-----------------------------7da2de1c1c07e4
Content-Disposition: form-data; name="foo"; filename="tt.rar"
Content-Type: application/octet-stream
abc优希 2010-12-27
  • 打赏
  • 举报
回复
没有用structs ,就是一般的例子,学习
magong 2010-12-27
  • 打赏
  • 举报
回复
同意1楼,用上传组件吧。
自己解析参数很麻烦的。
goodsun00 2010-12-27
  • 打赏
  • 举报
回复
用structs 里面有个fileupload来处理,文件用FormFile存,查下API 有很多方法,有getfilename()方法可以使用
kebin0001 2010-12-27
  • 打赏
  • 举报
回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧