文件下载:

@GET
@Path("/fileGet")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFile(@QueryParam("fileName") String fileName) {
String filepath = configs.getProperty("filePath");
String totalFile = filepath + File.separator + fileName;
logger.info("准备下载文件:" + totalFile);
File file = new File(totalFile);
long fileLength = file.length();
ResponseBuilder responseBuilder = Response.ok(file,
MediaType.APPLICATION_OCTET_STREAM);
responseBuilder.type("application/x-msdownload");
Response response = null;
try {
responseBuilder
.header("Content-Disposition", "attachment; filename="
+ URLEncoder.encode(getFileName(fileName), "UTF-8"));
responseBuilder.header("Content-Length", Long.toString(fileLength));
response = responseBuilder.build();
return response;
} catch (UnsupportedEncodingException e) {
logger.error("设置header失败:" + fileName);
e.printStackTrace();
}
return null;

}

 

上传:

@Path("/uploadData")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@POST
@Produces(MediaType.TEXT_PLAIN)
public String uploadData(@Context HttpServletRequest request,
@Context HttpServletResponse response) {
System.out.println("nihao");
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
String msg = "上传失败!";
Boolean flag = false;
Map<String, String> map = new HashMap<String, String>();

String fileName = "";
String fileFormat = "";
String saveFilePath = "";
String fileNewName = "";
String attId = "";
String fieldNameId = "";
if (isMultipart) {
// 构造一个文件上传处理对象
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8"); // 支持中文文件名
List list = new ArrayList<FileItem>();
try {
// 解析表单中提交的所有文件内容
list = upload.parseRequest(request);
for (int i = 0; i < list.size(); i++) {
FileItem item = (FileItem) list.get(i);
if (item.isFormField()) { // 普通表单值
map.put(item.getFieldName(), item.getString("UTF-8"));
} else {
String name = item.getName(); // 获得上传的文件名(IE上是文件全路径,火狐等浏览器仅文件名)
fieldNameId = item.getFieldName();
fileName = name.substring(name.lastIndexOf('\\') + 1,
name.length());
/*fileFormat = fileName.substring(fileName
.lastIndexOf(".")); // 文件扩展名
*/ String uuidStr = UUID.randomUUID().toString();
fileNewName = uuidStr + fileFormat;
logger.info("");
flag = upload4Stream(fileNewName, "/root/qbtapp",
item.getInputStream()); // 上传文件
if (flag) {
msg = "上传成功!";
}
}
}

} catch (Exception e) {

}

return null;
}
return null;
}

private boolean upload4Stream(String fileName, String filePath,
InputStream inStream) {
boolean result = false;
if ((filePath == null) || (filePath.trim().length() == 0)) {
return result;
}
OutputStream outStream = null;
try {
String wholeFilePath = filePath + "\\" + fileName;
System.out.println(wholeFilePath);
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
File outputFile = new File(wholeFilePath);
boolean isFileExist = outputFile.exists();
boolean canUpload = true;
if (isFileExist) {
canUpload = outputFile.delete();
}
if (canUpload) {
int available = 0;
outStream = new BufferedOutputStream(new FileOutputStream(
outputFile), 2048);
byte[] buffer = new byte[2048];
while ((available = inStream.read(buffer)) > 0) {
if (available < 2048)
outStream.write(buffer, 0, available);
else {
outStream.write(buffer, 0, 2048);
}
}
result = true;
}
} catch (Exception e) {
e.printStackTrace();
try {
if (inStream != null) {
inStream.close();
}
if (outStream != null)
outStream.close();
} catch (Exception ex) {
e.printStackTrace();
}
} finally {
try {
if (inStream != null) {
inStream.close();
}
if (outStream != null)
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(result);
return result;
}