gpt4 book ai didi

java - 如何使用ExtJs向服务器发送参数?

转载 作者:行者123 更新时间:2023-12-01 15:12:23 25 4
gpt4 key购买 nike

我要将文件上传到服务器并发送参数。但我有两个问题。

1 无法发送参数。我愿意:

      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();
}
}

但在控制台中仅获取st=null

2 无法在文件上传面板中使用组合框:

var x = new Ext.Window({
title:'Загрузка файла',
items:[
formp = new Ext.FormPanel({
fileUpload: true,
width: 350,
autoHeight: true,
bodyStyle: 'padding: 10px 10px 10px 10px;',
labelWidth: 70,
defaults: {
anchor: '95%',
allowBlank: false,
msgTarget: 'side'
},
items:[{
xtype:"combo",
fieldLabel:'Тип файла ',
name:"cb_file",
id:"cb_file",
mode:"local",
typeAhead: false,
loadingText: 'Загрузка...',
store:new Ext.data.SimpleStore({
fields: ['file_name', 'file_type'],
data : [['*.MIF/MID', 'mif'],['*.GPX', 'gpx']]
}),
forceSelection:true,
emptyText:'выбирите тип...',
triggerAction:'all',
valueField:'file_type',
displayField:'file_name',
anchor:'60%'
},{
xtype: 'fileuploadfield',
id: 'filedata',
emptyText: 'Выберите файл для загрузки...',
fieldLabel: 'Имя файла',
buttonText: 'Обзор'
}],
buttons: [{
text: 'Загрузить',
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();
}
})
}
}]
})
]
})
x.show();

如果我在服务器端执行此操作,则没有任何效果。例如,如果我上传的不是 zip 存档,我会收到警告,提示它不是 zip 文件,但无法获取它。如果我不在面板上添加组合框,我会收到此警报。怎么了?

最佳答案

那是因为该请求是多部分请求。然后,您无法使用以下方式读取参数: String st = request.getParameter("file_type");

因为它始终为空。相反,请使用以下片段:

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);
}
}

关于你的第二个问题,我不明白。

关于java - 如何使用ExtJs向服务器发送参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12154417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com