Java 如何使用ExtJs将参数发送到服务器?
Im将文件上载到服务器并发送参数。但我有两个问题 1无法发送参数。我有:Java 如何使用ExtJs将参数发送到服务器?,java,javascript,servlets,extjs,Java,Javascript,Servlets,Extjs,Im将文件上载到服务器并发送参数。但我有两个问题 1无法发送参数。我有: handler: function(){ mapinfo="mapinfo"; formp.getForm().submit({ url: url_servlet+'uploadfile', params: {file_type: mapinfo}, success: function(formp, o) {
handler: function(){
mapinfo="mapinfo";
formp.getForm().submit({
url: url_servlet+'uploadfile',
params: {file_type: mapinfo},
success: function(formp, o) {
alert(o.result.file);
kad_tab.getStore().reload()
zoom_store.load();
}
})
}
和服务器端:
public class uploadfile extends HttpServlet implements Servlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
String st = request.getParameter("file_type");
PrintWriter writer = response.getWriter();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
return;
}
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> list=null;
String mifpath= "1";
String path = " ";
String mif = " ";
String from = "\\\\";
String to ="/";
String error="";
try{
list = upload.parseRequest(request);
Iterator<FileItem> it = list.iterator();
response.setContentType("text/html");
while ( it.hasNext() )
{
FileItem item = (FileItem) it.next();
File disk = new File("C:/uploaded_files/"+item.getName());
path = disk.toString();
String code = new String(path.substring(path.lastIndexOf("."), path.length()).getBytes("ISO-8859-1"),"utf-8");
if (code.equalsIgnoreCase(".zip"))
{
mifpath=path;
mif = mifpath.replaceAll(from, to);
item.write(disk);
//error=unzip.unpack(mif, "C:/uploaded_files");
}
else
{
error = "Выбранный файл не является архивом zip";
}
}
}
catch ( Exception e ) {
log( "Upload Error" , e);
}
System.out.println("st="+st);
writer.println("{success:true, file:'"+error+"'}");
writer.close();
}
}
如果我在服务器端执行此操作,则不会有任何效果。例如,如果我上传的不是zip文件,我会得到警告,说它不是zip文件,但没有得到它。如果我没有在面板上添加combobox,我会收到此警报。怎么了?这是因为请求是多部分的。然后,您不能使用以下命令读取参数: 字符串st=request.getParameter(“文件类型”) 因为它总是空的。相反,请使用以下snipet:
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
processFormField(item);
} else {
processUploadedFile(item);
}
}
关于你的第二个问题,我不明白
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
processFormField(item);
} else {
processUploadedFile(item);
}
}