两个grails项目,一个为客户端,一个为服务端,客户端有保存图面的功能,图片传送到服务端保存。代码如下
客户端:Controller
def saveFile() {
println("app启动页广告")
def file = request.getFile("file")
if (!file || file.empty) {
throw new UploadFileException("上传文件不能为空")
}
String fileName = file.originalFilename
if (!fileName?.endsWith(".jpg") && !fileName?.endsWith(".gif") && !fileName?.endsWith(".png") && !fileName?.endsWith(".jpeg")) {
throw new NoPermitOperationException("只允许上传jpg, gif, png格式的图片")
}
adImageService.uploadFileToBCPush(file)
render([code: 0, message: "图片上传成功"] as JSON)
service
def uploadFileToBCPush(CommonsMultipartFile picFile) {
if (!picFile?.size) {
throw new NoPermitOperationException("错误的图片文件")
}
def fileName = picFile.originalFilename
def result = uploadPic(fileName,picFile,"http://localhost:9080/BCPush/adImageFromBC/receive.json")
if(result != "1"){
throw new RuntimeException("上传图片失败")
}
}
def uploadPic(String fileName, CommonsMultipartFile picFile, String url){
//将CommonsMultipartFile 转换成file
DiskFileItem fi = (DiskFileItem)picFile.getFileItem();
File f = fi.getStoreLocation();
HttpClient hc = new HttpClient()
def result = httpPost(url,fileName,f)
if(result.result){
return "1"
}else{
return "-1"
}
}
def httpPost(String url, String fileName, File file){
JSONObject jsonResult = new JSONObject();
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
MultipartEntity entity = new MultipartEntity();
entity.addPart("Content-Type",)
entity.addPart("fileName",new StringBody(fileName,Charset.forName("UTF-8")))
FileBody fb = new FileBody(file);
entity.addPart("file",fb)
post.setEntity(entity)
HttpResponse result = httpclient.execute(post);
def str;
/**读取服务器返回过来的json字符串数据**/
str = EntityUtils.toString(result.getEntity(),"utf-8");
if (result.getStatusLine().getStatusCode() == 200) {
/**把json字符串转换成json对象**/
jsonResult = JSONObject.fromObject(str);
}
EntityUtils.consume()
return jsonResult;
}
服务端就一个Controller,现在没有处理,只是一个接收,但是为空
def receive(MultipartFile file){
File dir = new File("C:\\img");
if(!dir.exists()){
dir.mkdirs();
}
def fileName = file.getOriginalFilename()
File serverFile = new File(dir+"、"+fileName)
}
下面是debug截图
根据你提供的代码,我注意到在客户端的上传文件方法中,你使用了CommonsMultipartFile
来接收文件数据,然后将其转换为File
对象,并通过HTTP POST请求将该文件发送到服务端。而在服务端的接收方法中,则使用了MultipartFile
作为参数。
这可能导致客户端上传的文件在服务端接收时为空。为了解决这个问题,你需要确保客户端和服务端都使用相同的文件对象类型。在Grails中,可以使用org.springframework.web.multipart.MultipartFile
实例来处理上传文件。
下面是修正后的代码示例:
客户端的Controller:
import org.springframework.web.multipart.MultipartFile
def saveFile() {
println("app启动页广告")
MultipartFile file = request.getFile("file") // 修改此处的类型为 MultipartFile
if (!file || file.isEmpty()) {
throw new UploadFileException("上传文件不能为空")
}
String fileName = file.originalFilename
if (!(fileName.endsWith(".jpg") || fileName.endsWith(".gif") || fileName.endsWith(".png") || fileName.endsWith(".jpeg"))) {
throw new NoPermitOperationException("只允许上传jpg、gif、png格式的图片")
}
adImageService.uploadFileToBCPush(file)
render([code: 0, message: "图片上传成功"] as JSON)
}
// 接下来的方法不变
服务端的Controller:
import org.springframework.web.multipart.MultipartFile
def receive(MultipartFile file){ // 修改此处的类型为 MultipartFile
File dir = new File("C:\\img")
if(!dir.exists()){
dir.mkdirs()
}
String fileName = file.originalFilename
File serverFile = new File(dir, fileName) // 修改此处为逗号分隔的参数,而不是中文逗号
// 其他处理逻辑...
}