JSP uses ajaxFileUpload. js to implement cross domain issues
- 2021-09-20 21:13:33
- OfStack
Don't talk too much nonsense, just post the code for everyone.
The jsp code is as follows
$.ajaxFileUpload
(
{
url:'http://lh.abc.com:8080/gap/gap/fileUpload.do',// Server-side request address for file upload (native is fxb.abc.com )
secureuri:false,//1 Set to false
fileElementId:'file',// Of the file upload space id Attribute <input type="file" id="file" name="file" />
dataType: 'jsonp',// Return value type 1 Set to json
jsonp: 'jsoncallback',
jsonpCallback:'success_jsonpCallback',
function success_jsonpCallback(data) {
alert("1");
},
success: function (data, status) // Server Success Response Handler
{
alert(data.message);// Returned from the server json Take out in message Data in , Among them message For in struts2 Medium action Member variables defined in
if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
alert(data.message);
}
}
},
error: function (data, status, e)// Server response failure handler
{
alert(status);
alert(e);
}
}
)
Configuration file
<action name="fileUpload" class="com.gap.action.FileUploadAction" method="fileUpload">
<result type="json" name="success">
<param name="contentType">
text/html
</param>
</result>
<result type="json" name="error">
<param name="contentType">
text/html
</param>
</result>
</action>
The processing in action is as follows
public String fileUpload() throws Exception {
String path = ServletActionContext.getRequest().getRealPath("/upload1");
// String path = ConfigDataInfo.getConfigValue("imgServer");
try {
File f = this.getFile();
if (this.getFileFileName().endsWith(".exe")) {
message = " I'm sorry , The file format you uploaded is not allowed !!!";
} else {
FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path + "/"
+ this.getFileFileName());
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
message = " Upload succeeded ";
}
} catch (Exception e) {
e.printStackTrace();
message = " I'm sorry , File upload failed !!!!";
}
return SUCCESS;
}
Every cross-domain upload pictures, can be successfully uploaded to the server, but can not return information correctly, always enter error method, correct should enter success method