使用外部jar包吧。org.apache.commons.fileupload这个可以 自己查下资料
解决方案 »
- 麻烦各位前辈们帮我看下这个程序哪出错了
- JSP文件上传组件
- 访问webservice ,代码里面有,call.setOperationName(new QName("","CreateBusinessData"));setOperationName是什么意思
- J2EE项目在Linux下开发与在WINDOWS下开发的区别,各自的优势?
- struts2 找不到action的诡异问题 --并非普通的类型
- struts中的session问题
- 只安装了j2se_sdk是不是不能编写j2ee程序呀?(请多指教)
- 如何用实现对黑名单中的网址进行拦截
- 问:怎么把java的.jar程序在linux上注册成服务,设置开机自启动
- 关于接口开发
- 网页抓取.
- 求一份servlet api文档,chm格式的!!
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
item.getInputStream();
if (!item.isFormField()) {
//文件流
}else{
//非文件流
String value=item.getString();
value = new String(value.getBytes("ISO-8859-1"),"UTF-8");
}
这样确实有点麻烦,你最好是把文件上传的form和常规数据的form分开提交,
而且比如Tomcat的Servlet标准实现里也没有对于这样二进制数据的获取实现,所以你得自己解析了。有很多开源的上传文件的jar包可以用,你搜一下就有了。我这里提供一个实现你参考一下。
public class FileFormAnalysis implements FormAnalysis { private static final LogUtil LOG = new LogUtil(LogFactory.getLog(
FileFormAnalysis.class)); /**
* 获取上传各文件文件名所在域的字节数组的迭代器
* @param request 请求
* @return 字节数组的迭代器
* @throws IOException
*/
@Override
public Iterator<Field> analysis(HttpServletRequest request)
throws IOException { InputStream input = request.getInputStream();
//没有设定文件字符集的话,默认是'iso8859_1'
String charset = UploadBytes.getCharset(request);
//获取表单域的分隔符
byte[] boundarys = UploadBytes.getBoundaryStr(request).getBytes(charset); return new FieldIterator(input, charset, boundarys);
} /**
* 获取上传文件迭代器的实现
*/
private class FieldIterator implements Iterator<Field> { private String charset;
private byte[] boundarys;
private InputStream input;
private ByteArrayOutputStream output;
private Field currentField;
private List<Field> currentNextFields;
int num = 0; private FieldIterator(InputStream input, String charset,
byte[] boundarys) {
this.input = input;
this.charset = charset;
this.boundarys = boundarys; this.output = new ByteArrayOutputStream();
} @Override
public boolean hasNext() {
currentField = null;//初始当前域置为空
//如果一次取得域有多个
if (currentNextFields != null && !currentNextFields.isEmpty()) {
currentField = currentNextFields.remove(0);
return true;
}
int r = -1;
byte[] buff = new byte[1024];
List<byte[]> fieldList = null;
List<byte[]> fileList = null; try {
while ((r = input.read(buff)) != -1) {
output.write(buff, 0, r); //获取上传文件的各域的字节列表
fieldList = UploadBytes.findFiled(boundarys, output); if (fieldList != null && !fieldList.isEmpty()) {
//获取实际上传文件字节列表
fileList = UploadBytes.getFilesByteList(fieldList,
charset);
if (fileList != null && !fileList.isEmpty()) {
break;
}
}
} if (fileList == null || fileList.isEmpty()) {
return false;
}
if (fileList.size() > 1) {//如果该长度范围有多个域时
currentNextFields = new ArrayList<Field>(fileList.size());
}
Field field = null;
for (int i = 0; i < fileList.size(); i++) {
//获取上传文件的实际内容的Field对象
field = UploadBytes.getField(fileList.get(i), charset); if (i == 0) {//该范围长度有一个域的情况
currentField = field;
} else {//如果该长度范围有多个域的情况
currentNextFields.add(field);
}
}
} catch (Exception ex) {
LOG.errorLog(ex);
return false;
} return currentField == null ? false : true;
} @Override
public Field next() {
return this.currentField;
} @Override
public void remove() {
throw new UnsupportedOperationException();
}
}
}
至于原理嘛,简单的说下。<form enctype="multipart/form-data" action="etnetChina/etnetUpload" method="post">
<input type="file" name="file"/>
<input type="text" name="text" />
<input type="submit" value="上传文件" />
</form>
上面是一个表单,同样multipart/form-data。
假设name="text"这个域我点击了一个文本文件,内容是。text
This upload test.
所以提交的http请求大概是这样的。POST /Upload HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.2.8) Gecko/20100723 Ubuntu/10.04 (lucid) Firefox/3.6.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------205147955511612195381301874305
Content-Length: 243
-----------------------------205147955511612195381301874305
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain
[\r\n]
text
This upload test.
-----------------------------205147955511612195381301874305
Content-Disposition: form-data; name="text"
[\r\n]
test
-----------------------------205147955511612195381301874305--
[\r\n]
上面的代码就是解析这些东东。其中
boundary=---------------------------205147955511612195381301874305
表示了这些各个域之间的分隔是什么。简单就是这些。
request.setCharacterEncoding("UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
List items = null;
items = upload.parseRequest(request);
Iterator itrTwo = items.iterator();
String path = "";
while(itrTwo.hasNext()){
FileItem item = (FileItem) itr.next();
if(item.isFormField()){
if(item.getFieldName().equals("path")){
path = new String(item.getString().getBytes("iso8859-1"),"utf-8");;
break;
}
}
}摘了一部分
List fileItems = upload.parseRequest(request); 中的upload是什么?
String name=mySmartUpload.getRequest().getParameter("name");